1/*
2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
4 *
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 *
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, Joerg Wunsch.
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
19 *
20 * $Id: if_spppsubr.c,v 1.29 1997/12/15 20:31:08 eivind Exp $
21 */
22
23#include "opt_ipx.h"
24
25#include <sys/param.h>
26#include <sys/systm.h>
27#include <sys/kernel.h>
28#include <sys/sockio.h>
29#include <sys/socket.h>
30#include <sys/syslog.h>
31#include <sys/malloc.h>
32#include <sys/mbuf.h>
33#include <sys/md5.h>
34
35#include <net/if.h>
36#include <net/netisr.h>
37#include <net/if_types.h>
38
39#include <machine/stdarg.h>
40
41#ifdef INET
42#include <netinet/in.h>
43#include <netinet/in_systm.h>
44#include <netinet/in_var.h>
45#include <netinet/ip.h>
46#include <netinet/tcp.h>
47#include <netinet/if_ether.h>
48#endif
49
50#ifdef IPX
51#include <netipx/ipx.h>
52#include <netipx/ipx_if.h>
53#endif
54
55#ifdef NS
56#include <netns/ns.h>
57#include <netns/ns_if.h>
58#endif
59
60#ifdef ISO
61#include <netiso/argo_debug.h>
62#include <netiso/iso.h>
63#include <netiso/iso_var.h>
64#include <netiso/iso_snpac.h>
65#endif
66
67#include <net/if_sppp.h>
68
69#define MAXALIVECNT 3 /* max. alive packets */
70
71/*
72 * Interface flags that can be set in an ifconfig command.
73 *
74 * Setting link0 will make the link passive, i.e. it will be marked
75 * as being administrative openable, but won't be opened to begin
76 * with. Incoming calls will be answered, or subsequent calls with
77 * -link1 will cause the administrative open of the LCP layer.
78 *
79 * Setting link1 will cause the link to auto-dial only as packets
80 * arrive to be sent.
81 *
82 * Setting IFF_DEBUG will syslog the option negotiation and state
83 * transitions at level kern.debug. Note: all logs consistently look
84 * like
85 *
86 * <if-name><unit>: <proto-name> <additional info...>
87 *
88 * with <if-name><unit> being something like "bppp0", and <proto-name>
89 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
90 */
91
92#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
93#define IFF_AUTO IFF_LINK1 /* auto-dial on output */
94
95#define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
96#define PPP_UI 0x03 /* Unnumbered Information */
97#define PPP_IP 0x0021 /* Internet Protocol */
98#define PPP_ISO 0x0023 /* ISO OSI Protocol */
99#define PPP_XNS 0x0025 /* Xerox NS Protocol */
100#define PPP_IPX 0x002b /* Novell IPX Protocol */
101#define PPP_LCP 0xc021 /* Link Control Protocol */
102#define PPP_PAP 0xc023 /* Password Authentication Protocol */
103#define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
104#define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
105
106#define CONF_REQ 1 /* PPP configure request */
107#define CONF_ACK 2 /* PPP configure acknowledge */
108#define CONF_NAK 3 /* PPP configure negative ack */
109#define CONF_REJ 4 /* PPP configure reject */
110#define TERM_REQ 5 /* PPP terminate request */
111#define TERM_ACK 6 /* PPP terminate acknowledge */
112#define CODE_REJ 7 /* PPP code reject */
113#define PROTO_REJ 8 /* PPP protocol reject */
114#define ECHO_REQ 9 /* PPP echo request */
115#define ECHO_REPLY 10 /* PPP echo reply */
116#define DISC_REQ 11 /* PPP discard request */
117
118#define LCP_OPT_MRU 1 /* maximum receive unit */
119#define LCP_OPT_ASYNC_MAP 2 /* async control character map */
120#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
121#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
122#define LCP_OPT_MAGIC 5 /* magic number */
123#define LCP_OPT_RESERVED 6 /* reserved */
124#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
125#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
126
127#define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
128#define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
129#define IPCP_OPT_ADDRESS 3 /* local IP address */
130
131#define PAP_REQ 1 /* PAP name/password request */
132#define PAP_ACK 2 /* PAP acknowledge */
133#define PAP_NAK 3 /* PAP fail */
134
135#define CHAP_CHALLENGE 1 /* CHAP challenge request */
136#define CHAP_RESPONSE 2 /* CHAP challenge response */
137#define CHAP_SUCCESS 3 /* CHAP response ok */
138#define CHAP_FAILURE 4 /* CHAP response failed */
139
140#define CHAP_MD5 5 /* hash algorithm - MD5 */
141
142#define CISCO_MULTICAST 0x8f /* Cisco multicast address */
143#define CISCO_UNICAST 0x0f /* Cisco unicast address */
144#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
145#define CISCO_ADDR_REQ 0 /* Cisco address request */
146#define CISCO_ADDR_REPLY 1 /* Cisco address reply */
147#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
148
149/* states are named and numbered according to RFC 1661 */
150#define STATE_INITIAL 0
151#define STATE_STARTING 1
152#define STATE_CLOSED 2
153#define STATE_STOPPED 3
154#define STATE_CLOSING 4
155#define STATE_STOPPING 5
156#define STATE_REQ_SENT 6
157#define STATE_ACK_RCVD 7
158#define STATE_ACK_SENT 8
159#define STATE_OPENED 9
160
161struct ppp_header {
162 u_char address;
163 u_char control;
164 u_short protocol;
165};
166#define PPP_HEADER_LEN sizeof (struct ppp_header)
167
168struct lcp_header {
169 u_char type;
170 u_char ident;
171 u_short len;
172};
173#define LCP_HEADER_LEN sizeof (struct lcp_header)
174
175struct cisco_packet {
176 u_long type;
177 u_long par1;
178 u_long par2;
179 u_short rel;
180 u_short time0;
181 u_short time1;
182};
183#define CISCO_PACKET_LEN 18
184
185/*
186 * We follow the spelling and capitalization of RFC 1661 here, to make
187 * it easier comparing with the standard. Please refer to this RFC in
188 * case you can't make sense out of these abbreviation; it will also
189 * explain the semantics related to the various events and actions.
190 */
191struct cp {
192 u_short proto; /* PPP control protocol number */
193 u_char protoidx; /* index into state table in struct sppp */
194 u_char flags;
195#define CP_LCP 0x01 /* this is the LCP */
196#define CP_AUTH 0x02 /* this is an authentication protocol */
197#define CP_NCP 0x04 /* this is a NCP */
198#define CP_QUAL 0x08 /* this is a quality reporting protocol */
199 const char *name; /* name of this control protocol */
200 /* event handlers */
201 void (*Up)(struct sppp *sp);
202 void (*Down)(struct sppp *sp);
203 void (*Open)(struct sppp *sp);
204 void (*Close)(struct sppp *sp);
205 void (*TO)(void *sp);
206 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
207 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
208 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
209 /* actions */
210 void (*tlu)(struct sppp *sp);
211 void (*tld)(struct sppp *sp);
212 void (*tls)(struct sppp *sp);
213 void (*tlf)(struct sppp *sp);
214 void (*scr)(struct sppp *sp);
215};
216
217static struct sppp *spppq;
218static struct callout_handle keepalive_ch;
219
220/*
221 * The following disgusting hack gets around the problem that IP TOS
222 * can't be set yet. We want to put "interactive" traffic on a high
223 * priority queue. To decide if traffic is interactive, we check that
224 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
225 *
226 * XXX is this really still necessary? - joerg -
227 */
228static u_short interactive_ports[8] = {
229 0, 513, 0, 0,
230 0, 21, 0, 23,
231};
232#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
233
234/* almost every function needs these */
235#define STDDCL \
236 struct ifnet *ifp = &sp->pp_if; \
237 int debug = ifp->if_flags & IFF_DEBUG
238
239static int sppp_output(struct ifnet *ifp, struct mbuf *m,
240 struct sockaddr *dst, struct rtentry *rt);
241
242static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
243static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
244
245static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
246 struct mbuf *m);
247static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
248 u_char ident, u_short len, void *data);
249static void sppp_cp_timeout(void *arg);
250static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
251 int newstate);
252static void sppp_auth_send(const struct cp *cp,
253 struct sppp *sp, u_char type, u_char id,
254 ...);
255
256static void sppp_up_event(const struct cp *cp, struct sppp *sp);
257static void sppp_down_event(const struct cp *cp, struct sppp *sp);
258static void sppp_open_event(const struct cp *cp, struct sppp *sp);
259static void sppp_close_event(const struct cp *cp, struct sppp *sp);
260static void sppp_to_event(const struct cp *cp, struct sppp *sp);
261
262static void sppp_null(struct sppp *sp);
263
264static void sppp_lcp_init(struct sppp *sp);
265static void sppp_lcp_up(struct sppp *sp);
266static void sppp_lcp_down(struct sppp *sp);
267static void sppp_lcp_open(struct sppp *sp);
268static void sppp_lcp_close(struct sppp *sp);
269static void sppp_lcp_TO(void *sp);
270static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
271static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
272static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
273static void sppp_lcp_tlu(struct sppp *sp);
274static void sppp_lcp_tld(struct sppp *sp);
275static void sppp_lcp_tls(struct sppp *sp);
276static void sppp_lcp_tlf(struct sppp *sp);
277static void sppp_lcp_scr(struct sppp *sp);
278static void sppp_lcp_check_and_close(struct sppp *sp);
279static int sppp_ncp_check(struct sppp *sp);
280
281static void sppp_ipcp_init(struct sppp *sp);
282static void sppp_ipcp_up(struct sppp *sp);
283static void sppp_ipcp_down(struct sppp *sp);
284static void sppp_ipcp_open(struct sppp *sp);
285static void sppp_ipcp_close(struct sppp *sp);
286static void sppp_ipcp_TO(void *sp);
287static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
288static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
289static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
290static void sppp_ipcp_tlu(struct sppp *sp);
291static void sppp_ipcp_tld(struct sppp *sp);
292static void sppp_ipcp_tls(struct sppp *sp);
293static void sppp_ipcp_tlf(struct sppp *sp);
294static void sppp_ipcp_scr(struct sppp *sp);
295
296static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
297static void sppp_pap_init(struct sppp *sp);
298static void sppp_pap_open(struct sppp *sp);
299static void sppp_pap_close(struct sppp *sp);
300static void sppp_pap_TO(void *sp);
301static void sppp_pap_my_TO(void *sp);
302static void sppp_pap_tlu(struct sppp *sp);
303static void sppp_pap_tld(struct sppp *sp);
304static void sppp_pap_scr(struct sppp *sp);
305
306static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
307static void sppp_chap_init(struct sppp *sp);
308static void sppp_chap_open(struct sppp *sp);
309static void sppp_chap_close(struct sppp *sp);
310static void sppp_chap_TO(void *sp);
311static void sppp_chap_tlu(struct sppp *sp);
312static void sppp_chap_tld(struct sppp *sp);
313static void sppp_chap_scr(struct sppp *sp);
314
315static const char *sppp_auth_type_name(u_short proto, u_char type);
316static const char *sppp_cp_type_name(u_char type);
317static const char *sppp_dotted_quad(u_long addr);
318static const char *sppp_ipcp_opt_name(u_char opt);
319static const char *sppp_lcp_opt_name(u_char opt);
320static const char *sppp_phase_name(enum ppp_phase phase);
321static const char *sppp_proto_name(u_short proto);
322static const char *sppp_state_name(int state);
323static int sppp_params(struct sppp *sp, int cmd, void *data);
324static int sppp_strnlen(u_char *p, int max);
325static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
326 u_long *srcmask);
327static void sppp_keepalive(void *dummy);
328static void sppp_phase_network(struct sppp *sp);
329static void sppp_print_bytes(const u_char *p, u_short len);
330static void sppp_print_string(const char *p, u_short len);
331static void sppp_qflush(struct ifqueue *ifq);
332static void sppp_set_ip_addr(struct sppp *sp, u_long src);
333
334/* our control protocol descriptors */
335const struct cp lcp = {
336 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
337 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
338 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
339 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
340 sppp_lcp_scr
341};
342
343const struct cp ipcp = {
344 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
345 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
346 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
347 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
348 sppp_ipcp_scr
349};
350
351const struct cp pap = {
352 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
353 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
354 sppp_pap_TO, 0, 0, 0,
355 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
356 sppp_pap_scr
357};
358
359const struct cp chap = {
360 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
361 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
362 sppp_chap_TO, 0, 0, 0,
363 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
364 sppp_chap_scr
365};
366
367const struct cp *cps[IDX_COUNT] = {
368 &lcp, /* IDX_LCP */
369 &ipcp, /* IDX_IPCP */
370 &pap, /* IDX_PAP */
371 &chap, /* IDX_CHAP */
372};
373
374
375/*
376 * Exported functions, comprising our interface to the lower layer.
377 */
378
379/*
380 * Process the received packet.
381 */
382void
383sppp_input(struct ifnet *ifp, struct mbuf *m)
384{
385 struct ppp_header *h;
386 struct ifqueue *inq = 0;
387 int s;
388 struct sppp *sp = (struct sppp *)ifp;
389 int debug = ifp->if_flags & IFF_DEBUG;
390
391 if (ifp->if_flags & IFF_UP)
392 /* Count received bytes, add FCS and one flag */
393 ifp->if_ibytes += m->m_pkthdr.len + 3;
394
395 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
396 /* Too small packet, drop it. */
397 if (debug)
398 log(LOG_DEBUG,
399 "%s%d: input packet is too small, %d bytes\n",
400 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
401 drop:
402 ++ifp->if_ierrors;
403 ++ifp->if_iqdrops;
404 m_freem (m);
405 return;
406 }
407
408 /* Get PPP header. */
409 h = mtod (m, struct ppp_header*);
410 m_adj (m, PPP_HEADER_LEN);
411
412 switch (h->address) {
413 case PPP_ALLSTATIONS:
414 if (h->control != PPP_UI)
415 goto invalid;
416 if (sp->pp_flags & PP_CISCO) {
417 if (debug)
418 log(LOG_DEBUG,
419 "%s%d: PPP packet in Cisco mode "
420 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
421 ifp->if_name, ifp->if_unit,
422 h->address, h->control, ntohs(h->protocol));
423 goto drop;
424 }
425 switch (ntohs (h->protocol)) {
426 default:
427 if (sp->state[IDX_LCP] == STATE_OPENED)
428 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
429 ++sp->pp_seq, m->m_pkthdr.len + 2,
430 &h->protocol);
431 if (debug)
432 log(LOG_DEBUG,
433 "%s%d: invalid input protocol "
434 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
435 ifp->if_name, ifp->if_unit,
436 h->address, h->control, ntohs(h->protocol));
437 ++ifp->if_noproto;
438 goto drop;
439 case PPP_LCP:
440 sppp_cp_input(&lcp, sp, m);
441 m_freem (m);
442 return;
443 case PPP_PAP:
444 if (sp->pp_phase >= PHASE_AUTHENTICATE)
445 sppp_pap_input(sp, m);
446 m_freem (m);
447 return;
448 case PPP_CHAP:
449 if (sp->pp_phase >= PHASE_AUTHENTICATE)
450 sppp_chap_input(sp, m);
451 m_freem (m);
452 return;
453#ifdef INET
454 case PPP_IPCP:
455 if (sp->pp_phase == PHASE_NETWORK)
456 sppp_cp_input(&ipcp, sp, m);
457 m_freem (m);
458 return;
459 case PPP_IP:
460 if (sp->state[IDX_IPCP] == STATE_OPENED) {
461 schednetisr (NETISR_IP);
462 inq = &ipintrq;
463 }
464 break;
465#endif
466#ifdef IPX
467 case PPP_IPX:
468 /* IPX IPXCP not implemented yet */
469 if (sp->pp_phase == PHASE_NETWORK) {
470 schednetisr (NETISR_IPX);
471 inq = &ipxintrq;
472 }
473 break;
474#endif
475#ifdef NS
476 case PPP_XNS:
477 /* XNS IDPCP not implemented yet */
478 if (sp->pp_phase == PHASE_NETWORK) {
479 schednetisr (NETISR_NS);
480 inq = &nsintrq;
481 }
482 break;
483#endif
484#ifdef ISO
485 case PPP_ISO:
486 /* OSI NLCP not implemented yet */
487 if (sp->pp_phase == PHASE_NETWORK) {
488 schednetisr (NETISR_ISO);
489 inq = &clnlintrq;
490 }
491 break;
492#endif
493 }
494 break;
495 case CISCO_MULTICAST:
496 case CISCO_UNICAST:
497 /* Don't check the control field here (RFC 1547). */
498 if (! (sp->pp_flags & PP_CISCO)) {
499 if (debug)
500 log(LOG_DEBUG,
501 "%s%d: Cisco packet in PPP mode "
502 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
503 ifp->if_name, ifp->if_unit,
504 h->address, h->control, ntohs(h->protocol));
505 goto drop;
506 }
507 switch (ntohs (h->protocol)) {
508 default:
509 ++ifp->if_noproto;
510 goto invalid;
511 case CISCO_KEEPALIVE:
512 sppp_cisco_input ((struct sppp*) ifp, m);
513 m_freem (m);
514 return;
515#ifdef INET
516 case ETHERTYPE_IP:
517 schednetisr (NETISR_IP);
518 inq = &ipintrq;
519 break;
520#endif
521#ifdef IPX
522 case ETHERTYPE_IPX:
523 schednetisr (NETISR_IPX);
524 inq = &ipxintrq;
525 break;
526#endif
527#ifdef NS
528 case ETHERTYPE_NS:
529 schednetisr (NETISR_NS);
530 inq = &nsintrq;
531 break;
532#endif
533 }
534 break;
535 default: /* Invalid PPP packet. */
536 invalid:
537 if (debug)
538 log(LOG_DEBUG,
539 "%s%d: invalid input packet "
540 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
541 ifp->if_name, ifp->if_unit,
542 h->address, h->control, ntohs(h->protocol));
543 goto drop;
544 }
545
546 if (! (ifp->if_flags & IFF_UP) || ! inq)
547 goto drop;
548
549 /* Check queue. */
550 s = splimp();
551 if (IF_QFULL (inq)) {
552 /* Queue overflow. */
553 IF_DROP(inq);
554 splx(s);
555 if (debug)
556 log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
557 ifp->if_name, ifp->if_unit);
558 goto drop;
559 }
560 IF_ENQUEUE(inq, m);
561 splx(s);
562}
563
564/*
565 * Enqueue transmit packet.
566 */
567static int
568sppp_output(struct ifnet *ifp, struct mbuf *m,
569 struct sockaddr *dst, struct rtentry *rt)
570{
571 struct sppp *sp = (struct sppp*) ifp;
572 struct ppp_header *h;
573 struct ifqueue *ifq;
574 int s, rv = 0;
575
576 s = splimp();
577
578 if ((ifp->if_flags & IFF_UP) == 0 ||
579 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
580 m_freem (m);
581 splx (s);
582 return (ENETDOWN);
583 }
584
585 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
586 /*
587 * Interface is not yet running, but auto-dial. Need
588 * to start LCP for it.
589 */
590 ifp->if_flags |= IFF_RUNNING;
591 splx(s);
592 lcp.Open(sp);
593 s = splimp();
594 }
595
596 ifq = &ifp->if_snd;
597#ifdef INET
598 /*
599 * Put low delay, telnet, rlogin and ftp control packets
600 * in front of the queue.
601 */
602 if (dst->sa_family == AF_INET) {
603 struct ip *ip = mtod (m, struct ip*);
604 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
605
606 if (! IF_QFULL (&sp->pp_fastq) &&
607 ((ip->ip_tos & IPTOS_LOWDELAY) ||
608 ip->ip_p == IPPROTO_TCP &&
609 m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
610 (INTERACTIVE (ntohs (tcp->th_sport)) ||
611 INTERACTIVE (ntohs (tcp->th_dport)))))
612 ifq = &sp->pp_fastq;
613 }
614#endif
615
616 /*
617 * Prepend general data packet PPP header. For now, IP only.
618 */
619 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
620 if (! m) {
621 if (ifp->if_flags & IFF_DEBUG)
622 log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
623 ifp->if_name, ifp->if_unit);
624 ++ifp->if_oerrors;
625 splx (s);
626 return (ENOBUFS);
627 }
628 h = mtod (m, struct ppp_header*);
629 if (sp->pp_flags & PP_CISCO) {
630 h->address = CISCO_UNICAST; /* unicast address */
631 h->control = 0;
632 } else {
633 h->address = PPP_ALLSTATIONS; /* broadcast address */
634 h->control = PPP_UI; /* Unnumbered Info */
635 }
636
637 switch (dst->sa_family) {
638#ifdef INET
639 case AF_INET: /* Internet Protocol */
640 if (sp->pp_flags & PP_CISCO)
641 h->protocol = htons (ETHERTYPE_IP);
642 else {
643 /*
644 * Don't choke with an ENETDOWN early. It's
645 * possible that we just started dialing out,
646 * so don't drop the packet immediately. If
647 * we notice that we run out of buffer space
648 * below, we will however remember that we are
649 * not ready to carry IP packets, and return
650 * ENETDOWN, as opposed to ENOBUFS.
651 */
652 h->protocol = htons(PPP_IP);
653 if (sp->state[IDX_IPCP] != STATE_OPENED)
654 rv = ENETDOWN;
655 }
656 break;
657#endif
658#ifdef NS
659 case AF_NS: /* Xerox NS Protocol */
660 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
661 ETHERTYPE_NS : PPP_XNS);
662 break;
663#endif
664#ifdef IPX
665 case AF_IPX: /* Novell IPX Protocol */
666 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
667 ETHERTYPE_IPX : PPP_IPX);
668 break;
669#endif
670#ifdef ISO
671 case AF_ISO: /* ISO OSI Protocol */
672 if (sp->pp_flags & PP_CISCO)
673 goto nosupport;
674 h->protocol = htons (PPP_ISO);
675 break;
676nosupport:
677#endif
678 default:
679 m_freem (m);
680 ++ifp->if_oerrors;
681 splx (s);
682 return (EAFNOSUPPORT);
683 }
684
685 /*
686 * Queue message on interface, and start output if interface
687 * not yet active.
688 */
689 if (IF_QFULL (ifq)) {
690 IF_DROP (&ifp->if_snd);
691 m_freem (m);
692 ++ifp->if_oerrors;
693 splx (s);
694 return (rv? rv: ENOBUFS);
695 }
696 IF_ENQUEUE (ifq, m);
697 if (! (ifp->if_flags & IFF_OACTIVE))
698 (*ifp->if_start) (ifp);
699
700 /*
701 * Count output packets and bytes.
702 * The packet length includes header, FCS and 1 flag,
703 * according to RFC 1333.
704 */
705 ifp->if_obytes += m->m_pkthdr.len + 3;
706 splx (s);
707 return (0);
708}
709
710void
711sppp_attach(struct ifnet *ifp)
712{
713 struct sppp *sp = (struct sppp*) ifp;
714
715 /* Initialize keepalive handler. */
716 if (! spppq)
717 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
718
719 /* Insert new entry into the keepalive list. */
720 sp->pp_next = spppq;
721 spppq = sp;
722
723 sp->pp_if.if_type = IFT_PPP;
724 sp->pp_if.if_output = sppp_output;
725 sp->pp_fastq.ifq_maxlen = 32;
726 sp->pp_cpq.ifq_maxlen = 20;
727 sp->pp_loopcnt = 0;
728 sp->pp_alivecnt = 0;
729 sp->pp_seq = 0;
730 sp->pp_rseq = 0;
731 sp->pp_phase = PHASE_DEAD;
732 sp->pp_up = lcp.Up;
733 sp->pp_down = lcp.Down;
734
735 sppp_lcp_init(sp);
736 sppp_ipcp_init(sp);
737 sppp_pap_init(sp);
738 sppp_chap_init(sp);
739}
740
741void
742sppp_detach(struct ifnet *ifp)
743{
744 struct sppp **q, *p, *sp = (struct sppp*) ifp;
745 int i;
746
747 /* Remove the entry from the keepalive list. */
748 for (q = &spppq; (p = *q); q = &p->pp_next)
749 if (p == sp) {
750 *q = p->pp_next;
751 break;
752 }
753
754 /* Stop keepalive handler. */
755 if (! spppq)
756 untimeout(sppp_keepalive, 0, keepalive_ch);
757
758 for (i = 0; i < IDX_COUNT; i++)
759 untimeout((cps[i])->TO, (void *)sp, sp->ch[i]);
760 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
761}
762
763/*
764 * Flush the interface output queue.
765 */
766void
767sppp_flush(struct ifnet *ifp)
768{
769 struct sppp *sp = (struct sppp*) ifp;
770
771 sppp_qflush (&sp->pp_if.if_snd);
772 sppp_qflush (&sp->pp_fastq);
773 sppp_qflush (&sp->pp_cpq);
774}
775
776/*
777 * Check if the output queue is empty.
778 */
779int
780sppp_isempty(struct ifnet *ifp)
781{
782 struct sppp *sp = (struct sppp*) ifp;
783 int empty, s;
784
785 s = splimp();
786 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
787 !sp->pp_if.if_snd.ifq_head;
788 splx(s);
789 return (empty);
790}
791
792/*
793 * Get next packet to send.
794 */
795struct mbuf *
796sppp_dequeue(struct ifnet *ifp)
797{
798 struct sppp *sp = (struct sppp*) ifp;
799 struct mbuf *m;
800 int s;
801
802 s = splimp();
803 /*
804 * Process only the control protocol queue until we have at
805 * least one NCP open.
806 *
807 * Do always serve all three queues in Cisco mode.
808 */
809 IF_DEQUEUE(&sp->pp_cpq, m);
810 if (m == NULL &&
811 (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) {
812 IF_DEQUEUE(&sp->pp_fastq, m);
813 if (m == NULL)
814 IF_DEQUEUE (&sp->pp_if.if_snd, m);
815 }
816 splx(s);
817 return m;
818}
819
820/*
821 * Pick the next packet, do not remove it from the queue.
822 */
823struct mbuf *
824sppp_pick(struct ifnet *ifp)
825{
826 struct sppp *sp = (struct sppp*)ifp;
827 struct mbuf *m;
828 int s;
829
830 s= splimp ();
831
832 m = sp->pp_cpq.ifq_head;
833 if (m == NULL &&
834 (sp->pp_phase == PHASE_NETWORK ||
835 (sp->pp_flags & PP_CISCO) != 0))
836 if ((m = sp->pp_fastq.ifq_head) == NULL)
837 m = sp->pp_if.if_snd.ifq_head;
838 splx (s);
839 return (m);
840}
841
842/*
843 * Process an ioctl request. Called on low priority level.
844 */
845int
846sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
847{
848 struct ifreq *ifr = (struct ifreq*) data;
849 struct sppp *sp = (struct sppp*) ifp;
850 int s, rv, going_up, going_down, newmode;
851
852 s = splimp();
853 rv = 0;
854 switch (cmd) {
855 case SIOCAIFADDR:
856 case SIOCSIFDSTADDR:
857 break;
858
859 case SIOCSIFADDR:
860 if_up(ifp);
861 /* fall through... */
862
863 case SIOCSIFFLAGS:
864 going_up = ifp->if_flags & IFF_UP &&
865 (ifp->if_flags & IFF_RUNNING) == 0;
866 going_down = (ifp->if_flags & IFF_UP) == 0 &&
867 ifp->if_flags & IFF_RUNNING;
868 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
869 if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
870 /* sanity */
871 newmode = IFF_PASSIVE;
872 ifp->if_flags &= ~IFF_AUTO;
873 }
874
875 if (going_up || going_down)
876 lcp.Close(sp);
877 if (going_up && newmode == 0) {
878 /* neither auto-dial nor passive */
879 ifp->if_flags |= IFF_RUNNING;
880 if (!(sp->pp_flags & PP_CISCO))
881 lcp.Open(sp);
882 } else if (going_down) {
883 sppp_flush(ifp);
884 ifp->if_flags &= ~IFF_RUNNING;
885 }
886
887 break;
888
889#ifdef SIOCSIFMTU
890#ifndef ifr_mtu
891#define ifr_mtu ifr_metric
892#endif
893 case SIOCSIFMTU:
894 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
895 return (EINVAL);
896 ifp->if_mtu = ifr->ifr_mtu;
897 break;
898#endif
899#ifdef SLIOCSETMTU
900 case SLIOCSETMTU:
901 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
902 return (EINVAL);
903 ifp->if_mtu = *(short*)data;
904 break;
905#endif
906#ifdef SIOCGIFMTU
907 case SIOCGIFMTU:
908 ifr->ifr_mtu = ifp->if_mtu;
909 break;
910#endif
911#ifdef SLIOCGETMTU
912 case SLIOCGETMTU:
913 *(short*)data = ifp->if_mtu;
914 break;
915#endif
916 case SIOCADDMULTI:
917 case SIOCDELMULTI:
918 break;
919
920 case SIOCGIFGENERIC:
921 case SIOCSIFGENERIC:
922 rv = sppp_params(sp, cmd, data);
923 break;
924
925 default:
926 rv = ENOTTY;
927 }
928 splx(s);
929 return rv;
930}
931
932
933/*
934 * Cisco framing implementation.
935 */
936
937/*
938 * Handle incoming Cisco keepalive protocol packets.
939 */
940static void
941sppp_cisco_input(struct sppp *sp, struct mbuf *m)
942{
943 STDDCL;
944 struct cisco_packet *h;
945 u_long me, mymask;
946
947 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
948 if (debug)
949 log(LOG_DEBUG,
950 "%s%d: cisco invalid packet length: %d bytes\n",
951 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
952 return;
953 }
954 h = mtod (m, struct cisco_packet*);
955 if (debug)
956 log(LOG_DEBUG,
957 "%s%d: cisco input: %d bytes "
958 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
959 ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
960 ntohl (h->type), h->par1, h->par2, h->rel,
961 h->time0, h->time1);
962 switch (ntohl (h->type)) {
963 default:
964 if (debug)
965 addlog("%s%d: cisco unknown packet type: 0x%lx\n",
966 ifp->if_name, ifp->if_unit, ntohl (h->type));
967 break;
968 case CISCO_ADDR_REPLY:
969 /* Reply on address request, ignore */
970 break;
971 case CISCO_KEEPALIVE_REQ:
972 sp->pp_alivecnt = 0;
973 sp->pp_rseq = ntohl (h->par1);
974 if (sp->pp_seq == sp->pp_rseq) {
975 /* Local and remote sequence numbers are equal.
976 * Probably, the line is in loopback mode. */
977 if (sp->pp_loopcnt >= MAXALIVECNT) {
978 printf ("%s%d: loopback\n",
979 ifp->if_name, ifp->if_unit);
980 sp->pp_loopcnt = 0;
981 if (ifp->if_flags & IFF_UP) {
982 if_down (ifp);
983 sppp_qflush (&sp->pp_cpq);
984 }
985 }
986 ++sp->pp_loopcnt;
987
988 /* Generate new local sequence number */
989 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
990 break;
991 }
992 sp->pp_loopcnt = 0;
993 if (! (ifp->if_flags & IFF_UP) &&
994 (ifp->if_flags & IFF_RUNNING)) {
995 if_up(ifp);
996 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
997 }
998 break;
999 case CISCO_ADDR_REQ:
1000 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1001 if (me != 0L)
1002 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1003 break;
1004 }
1005}
1006
1007/*
1008 * Send Cisco keepalive packet.
1009 */
1010static void
1011sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1012{
1013 STDDCL;
1014 struct ppp_header *h;
1015 struct cisco_packet *ch;
1016 struct mbuf *m;
1017 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1018
1019 MGETHDR (m, M_DONTWAIT, MT_DATA);
1020 if (! m)
1021 return;
1022 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1023 m->m_pkthdr.rcvif = 0;
1024
1025 h = mtod (m, struct ppp_header*);
1026 h->address = CISCO_MULTICAST;
1027 h->control = 0;
1028 h->protocol = htons (CISCO_KEEPALIVE);
1029
1030 ch = (struct cisco_packet*) (h + 1);
1031 ch->type = htonl (type);
1032 ch->par1 = htonl (par1);
1033 ch->par2 = htonl (par2);
1034 ch->rel = -1;
1035 ch->time0 = htons ((u_short) (t >> 16));
1036 ch->time1 = htons ((u_short) t);
1037
1038 if (debug)
1039 log(LOG_DEBUG,
1040 "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1041 ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
1042 ch->par2, ch->rel, ch->time0, ch->time1);
1043
1044 if (IF_QFULL (&sp->pp_cpq)) {
1045 IF_DROP (&sp->pp_fastq);
1046 IF_DROP (&ifp->if_snd);
1047 m_freem (m);
1048 } else
1049 IF_ENQUEUE (&sp->pp_cpq, m);
1050 if (! (ifp->if_flags & IFF_OACTIVE))
1051 (*ifp->if_start) (ifp);
1052 ifp->if_obytes += m->m_pkthdr.len + 3;
1053}
1054
1055/*
1056 * PPP protocol implementation.
1057 */
1058
1059/*
1060 * Send PPP control protocol packet.
1061 */
1062static void
1063sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1064 u_char ident, u_short len, void *data)
1065{
1066 STDDCL;
1067 struct ppp_header *h;
1068 struct lcp_header *lh;
1069 struct mbuf *m;
1070
1071 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1072 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1073 MGETHDR (m, M_DONTWAIT, MT_DATA);
1074 if (! m)
1075 return;
1076 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1077 m->m_pkthdr.rcvif = 0;
1078
1079 h = mtod (m, struct ppp_header*);
1080 h->address = PPP_ALLSTATIONS; /* broadcast address */
1081 h->control = PPP_UI; /* Unnumbered Info */
1082 h->protocol = htons (proto); /* Link Control Protocol */
1083
1084 lh = (struct lcp_header*) (h + 1);
1085 lh->type = type;
1086 lh->ident = ident;
1087 lh->len = htons (LCP_HEADER_LEN + len);
1088 if (len)
1089 bcopy (data, lh+1, len);
1090
1091 if (debug) {
1092 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
1093 ifp->if_name, ifp->if_unit,
1094 sppp_proto_name(proto),
1095 sppp_cp_type_name (lh->type), lh->ident,
1096 ntohs (lh->len));
1097 if (len)
1098 sppp_print_bytes ((u_char*) (lh+1), len);
1099 addlog(">\n");
1100 }
1101 if (IF_QFULL (&sp->pp_cpq)) {
1102 IF_DROP (&sp->pp_fastq);
1103 IF_DROP (&ifp->if_snd);
1104 m_freem (m);
1105 ++ifp->if_oerrors;
1106 } else
1107 IF_ENQUEUE (&sp->pp_cpq, m);
1108 if (! (ifp->if_flags & IFF_OACTIVE))
1109 (*ifp->if_start) (ifp);
1110 ifp->if_obytes += m->m_pkthdr.len + 3;
1111}
1112
1113/*
1114 * Handle incoming PPP control protocol packets.
1115 */
1116static void
1117sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1118{
1119 STDDCL;
1120 struct lcp_header *h;
1121 int len = m->m_pkthdr.len;
1122 int rv;
1123 u_char *p;
1124
1125 if (len < 4) {
1126 if (debug)
1127 log(LOG_DEBUG,
1128 "%s%d: %s invalid packet length: %d bytes\n",
1129 ifp->if_name, ifp->if_unit, cp->name, len);
1130 return;
1131 }
1132 h = mtod (m, struct lcp_header*);
1133 if (debug) {
1134 log(LOG_DEBUG,
1135 "%s%d: %s input(%s): <%s id=0x%x len=%d",
1136 ifp->if_name, ifp->if_unit, cp->name,
1137 sppp_state_name(sp->state[cp->protoidx]),
1138 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1139 if (len > 4)
1140 sppp_print_bytes ((u_char*) (h+1), len-4);
1141 addlog(">\n");
1142 }
1143 if (len > ntohs (h->len))
1144 len = ntohs (h->len);
1145 p = (u_char *)(h + 1);
1146 switch (h->type) {
1147 case CONF_REQ:
1148 if (len < 4) {
1149 if (debug)
1150 addlog("%s%d: %s invalid conf-req length %d\n",
1151 ifp->if_name, ifp->if_unit, cp->name,
1152 len);
1153 ++ifp->if_ierrors;
1154 break;
1155 }
1156 /* handle states where RCR doesn't get a SCA/SCN */
1157 switch (sp->state[cp->protoidx]) {
1158 case STATE_CLOSING:
1159 case STATE_STOPPING:
1160 return;
1161 case STATE_CLOSED:
1162 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1163 0, 0);
1164 return;
1165 }
1166 rv = (cp->RCR)(sp, h, len);
1167 switch (sp->state[cp->protoidx]) {
1168 case STATE_OPENED:
1169 (cp->tld)(sp);
1170 (cp->scr)(sp);
1171 /* fall through... */
1172 case STATE_ACK_SENT:
1173 case STATE_REQ_SENT:
1174 sppp_cp_change_state(cp, sp, rv?
1175 STATE_ACK_SENT: STATE_REQ_SENT);
1176 break;
1177 case STATE_STOPPED:
1178 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1179 (cp->scr)(sp);
1180 sppp_cp_change_state(cp, sp, rv?
1181 STATE_ACK_SENT: STATE_REQ_SENT);
1182 break;
1183 case STATE_ACK_RCVD:
1184 if (rv) {
1185 sppp_cp_change_state(cp, sp, STATE_OPENED);
1186 if (debug)
1187 log(LOG_DEBUG, "%s%d: %s tlu\n",
1188 ifp->if_name, ifp->if_unit,
1189 cp->name);
1190 (cp->tlu)(sp);
1191 } else
1192 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1193 break;
1194 default:
1195 printf("%s%d: %s illegal %s in state %s\n",
1196 ifp->if_name, ifp->if_unit, cp->name,
1197 sppp_cp_type_name(h->type),
1198 sppp_state_name(sp->state[cp->protoidx]));
1199 ++ifp->if_ierrors;
1200 }
1201 break;
1202 case CONF_ACK:
1203 if (h->ident != sp->confid[cp->protoidx]) {
1204 if (debug)
1205 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1206 ifp->if_name, ifp->if_unit, cp->name,
1207 h->ident, sp->confid[cp->protoidx]);
1208 ++ifp->if_ierrors;
1209 break;
1210 }
1211 switch (sp->state[cp->protoidx]) {
1212 case STATE_CLOSED:
1213 case STATE_STOPPED:
1214 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1215 break;
1216 case STATE_CLOSING:
1217 case STATE_STOPPING:
1218 break;
1219 case STATE_REQ_SENT:
1220 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1221 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1222 break;
1223 case STATE_OPENED:
1224 (cp->tld)(sp);
1225 /* fall through */
1226 case STATE_ACK_RCVD:
1227 (cp->scr)(sp);
1228 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1229 break;
1230 case STATE_ACK_SENT:
1231 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1232 sppp_cp_change_state(cp, sp, STATE_OPENED);
1233 if (debug)
1234 log(LOG_DEBUG, "%s%d: %s tlu\n",
1235 ifp->if_name, ifp->if_unit, cp->name);
1236 (cp->tlu)(sp);
1237 break;
1238 default:
1239 printf("%s%d: %s illegal %s in state %s\n",
1240 ifp->if_name, ifp->if_unit, cp->name,
1241 sppp_cp_type_name(h->type),
1242 sppp_state_name(sp->state[cp->protoidx]));
1243 ++ifp->if_ierrors;
1244 }
1245 break;
1246 case CONF_NAK:
1247 case CONF_REJ:
1248 if (h->ident != sp->confid[cp->protoidx]) {
1249 if (debug)
1250 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1251 ifp->if_name, ifp->if_unit, cp->name,
1252 h->ident, sp->confid[cp->protoidx]);
1253 ++ifp->if_ierrors;
1254 break;
1255 }
1256 if (h->type == CONF_NAK)
1257 (cp->RCN_nak)(sp, h, len);
1258 else /* CONF_REJ */
1259 (cp->RCN_rej)(sp, h, len);
1260
1261 switch (sp->state[cp->protoidx]) {
1262 case STATE_CLOSED:
1263 case STATE_STOPPED:
1264 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1265 break;
1266 case STATE_REQ_SENT:
1267 case STATE_ACK_SENT:
1268 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1269 (cp->scr)(sp);
1270 break;
1271 case STATE_OPENED:
1272 (cp->tld)(sp);
1273 /* fall through */
1274 case STATE_ACK_RCVD:
1275 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1276 (cp->scr)(sp);
1277 break;
1278 case STATE_CLOSING:
1279 case STATE_STOPPING:
1280 break;
1281 default:
1282 printf("%s%d: %s illegal %s in state %s\n",
1283 ifp->if_name, ifp->if_unit, cp->name,
1284 sppp_cp_type_name(h->type),
1285 sppp_state_name(sp->state[cp->protoidx]));
1286 ++ifp->if_ierrors;
1287 }
1288 break;
1289
1290 case TERM_REQ:
1291 switch (sp->state[cp->protoidx]) {
1292 case STATE_ACK_RCVD:
1293 case STATE_ACK_SENT:
1294 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1295 /* fall through */
1296 case STATE_CLOSED:
1297 case STATE_STOPPED:
1298 case STATE_CLOSING:
1299 case STATE_STOPPING:
1300 case STATE_REQ_SENT:
1301 sta:
1302 /* Send Terminate-Ack packet. */
1303 if (debug)
1304 log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1305 ifp->if_name, ifp->if_unit, cp->name);
1306 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1307 break;
1308 case STATE_OPENED:
1309 (cp->tld)(sp);
1310 sp->rst_counter[cp->protoidx] = 0;
1311 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1312 goto sta;
1313 break;
1314 default:
1315 printf("%s%d: %s illegal %s in state %s\n",
1316 ifp->if_name, ifp->if_unit, cp->name,
1317 sppp_cp_type_name(h->type),
1318 sppp_state_name(sp->state[cp->protoidx]));
1319 ++ifp->if_ierrors;
1320 }
1321 break;
1322 case TERM_ACK:
1323 switch (sp->state[cp->protoidx]) {
1324 case STATE_CLOSED:
1325 case STATE_STOPPED:
1326 case STATE_REQ_SENT:
1327 case STATE_ACK_SENT:
1328 break;
1329 case STATE_CLOSING:
1330 (cp->tlf)(sp);
1331 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1332 break;
1333 case STATE_STOPPING:
1334 (cp->tlf)(sp);
1335 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1336 break;
1337 case STATE_ACK_RCVD:
1338 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1339 break;
1340 case STATE_OPENED:
1341 (cp->tld)(sp);
1342 (cp->scr)(sp);
1343 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1344 break;
1345 default:
1346 printf("%s%d: %s illegal %s in state %s\n",
1347 ifp->if_name, ifp->if_unit, cp->name,
1348 sppp_cp_type_name(h->type),
1349 sppp_state_name(sp->state[cp->protoidx]));
1350 ++ifp->if_ierrors;
1351 }
1352 break;
1353 case CODE_REJ:
1354 case PROTO_REJ:
1355 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1356 log(LOG_INFO,
1357 "%s%d: %s: ignoring RXJ (%s) for proto 0x%x, "
1358 "danger will robinson\n",
1359 ifp->if_name, ifp->if_unit, cp->name,
1360 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1361 switch (sp->state[cp->protoidx]) {
1362 case STATE_CLOSED:
1363 case STATE_STOPPED:
1364 case STATE_REQ_SENT:
1365 case STATE_ACK_SENT:
1366 case STATE_CLOSING:
1367 case STATE_STOPPING:
1368 case STATE_OPENED:
1369 break;
1370 case STATE_ACK_RCVD:
1371 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1372 break;
1373 default:
1374 printf("%s%d: %s illegal %s in state %s\n",
1375 ifp->if_name, ifp->if_unit, cp->name,
1376 sppp_cp_type_name(h->type),
1377 sppp_state_name(sp->state[cp->protoidx]));
1378 ++ifp->if_ierrors;
1379 }
1380 break;
1381 case DISC_REQ:
1382 if (cp->proto != PPP_LCP)
1383 goto illegal;
1384 /* Discard the packet. */
1385 break;
1386 case ECHO_REQ:
1387 if (cp->proto != PPP_LCP)
1388 goto illegal;
1389 if (sp->state[cp->protoidx] != STATE_OPENED) {
1390 if (debug)
1391 addlog("%s%d: lcp echo req but lcp closed\n",
1392 ifp->if_name, ifp->if_unit);
1393 ++ifp->if_ierrors;
1394 break;
1395 }
1396 if (len < 8) {
1397 if (debug)
1398 addlog("%s%d: invalid lcp echo request "
1399 "packet length: %d bytes\n",
1400 ifp->if_name, ifp->if_unit, len);
1401 break;
1402 }
1403 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1404 /* Line loopback mode detected. */
1405 printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1406 if_down (ifp);
1407 sppp_qflush (&sp->pp_cpq);
1408
1409 /* Shut down the PPP link. */
1410 /* XXX */
1411 lcp.Down(sp);
1412 lcp.Up(sp);
1413 break;
1414 }
1415 *(long*)(h+1) = htonl (sp->lcp.magic);
1416 if (debug)
1417 addlog("%s%d: got lcp echo req, sending echo rep\n",
1418 ifp->if_name, ifp->if_unit);
1419 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1420 break;
1421 case ECHO_REPLY:
1422 if (cp->proto != PPP_LCP)
1423 goto illegal;
1424 if (h->ident != sp->lcp.echoid) {
1425 ++ifp->if_ierrors;
1426 break;
1427 }
1428 if (len < 8) {
1429 if (debug)
1430 addlog("%s%d: lcp invalid echo reply "
1431 "packet length: %d bytes\n",
1432 ifp->if_name, ifp->if_unit, len);
1433 break;
1434 }
1435 if (debug)
1436 addlog("%s%d: lcp got echo rep\n",
1437 ifp->if_name, ifp->if_unit);
1438 if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1439 sp->pp_alivecnt = 0;
1440 break;
1441 default:
1442 /* Unknown packet type -- send Code-Reject packet. */
1443 illegal:
1444 if (debug)
1445 addlog("%s%d: %c send code-rej for 0x%x\n",
1446 ifp->if_name, ifp->if_unit, cp->name, h->type);
1447 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1448 m->m_pkthdr.len, h);
1449 ++ifp->if_ierrors;
1450 }
1451}
1452
1453
1454/*
1455 * The generic part of all Up/Down/Open/Close/TO event handlers.
1456 * Basically, the state transition handling in the automaton.
1457 */
1458static void
1459sppp_up_event(const struct cp *cp, struct sppp *sp)
1460{
1461 STDDCL;
1462
1463 if (debug)
1464 log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1465 ifp->if_name, ifp->if_unit, cp->name,
1466 sppp_state_name(sp->state[cp->protoidx]));
1467
1468 switch (sp->state[cp->protoidx]) {
1469 case STATE_INITIAL:
1470 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1471 break;
1472 case STATE_STARTING:
1473 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1474 (cp->scr)(sp);
1475 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1476 break;
1477 default:
1478 printf("%s%d: %s illegal up in state %s\n",
1479 ifp->if_name, ifp->if_unit, cp->name,
1480 sppp_state_name(sp->state[cp->protoidx]));
1481 }
1482}
1483
1484static void
1485sppp_down_event(const struct cp *cp, struct sppp *sp)
1486{
1487 STDDCL;
1488
1489 if (debug)
1490 log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1491 ifp->if_name, ifp->if_unit, cp->name,
1492 sppp_state_name(sp->state[cp->protoidx]));
1493
1494 switch (sp->state[cp->protoidx]) {
1495 case STATE_CLOSED:
1496 case STATE_CLOSING:
1497 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1498 break;
1499 case STATE_STOPPED:
1500 (cp->tls)(sp);
1501 /* fall through */
1502 case STATE_STOPPING:
1503 case STATE_REQ_SENT:
1504 case STATE_ACK_RCVD:
1505 case STATE_ACK_SENT:
1506 sppp_cp_change_state(cp, sp, STATE_STARTING);
1507 break;
1508 case STATE_OPENED:
1509 (cp->tld)(sp);
1510 sppp_cp_change_state(cp, sp, STATE_STARTING);
1511 break;
1512 default:
1513 printf("%s%d: %s illegal down in state %s\n",
1514 ifp->if_name, ifp->if_unit, cp->name,
1515 sppp_state_name(sp->state[cp->protoidx]));
1516 }
1517}
1518
1519
1520static void
1521sppp_open_event(const struct cp *cp, struct sppp *sp)
1522{
1523 STDDCL;
1524
1525 if (debug)
1526 log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1527 ifp->if_name, ifp->if_unit, cp->name,
1528 sppp_state_name(sp->state[cp->protoidx]));
1529
1530 switch (sp->state[cp->protoidx]) {
1531 case STATE_INITIAL:
1532 (cp->tls)(sp);
1533 sppp_cp_change_state(cp, sp, STATE_STARTING);
1534 break;
1535 case STATE_STARTING:
1536 break;
1537 case STATE_CLOSED:
1538 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1539 (cp->scr)(sp);
1540 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1541 break;
1542 case STATE_STOPPED:
1543 case STATE_STOPPING:
1544 case STATE_REQ_SENT:
1545 case STATE_ACK_RCVD:
1546 case STATE_ACK_SENT:
1547 case STATE_OPENED:
1548 break;
1549 case STATE_CLOSING:
1550 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1551 break;
1552 }
1553}
1554
1555
1556static void
1557sppp_close_event(const struct cp *cp, struct sppp *sp)
1558{
1559 STDDCL;
1560
1561 if (debug)
1562 log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1563 ifp->if_name, ifp->if_unit, cp->name,
1564 sppp_state_name(sp->state[cp->protoidx]));
1565
1566 switch (sp->state[cp->protoidx]) {
1567 case STATE_INITIAL:
1568 case STATE_CLOSED:
1569 case STATE_CLOSING:
1570 break;
1571 case STATE_STARTING:
1572 (cp->tlf)(sp);
1573 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1574 break;
1575 case STATE_STOPPED:
1576 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1577 break;
1578 case STATE_STOPPING:
1579 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1580 break;
1581 case STATE_OPENED:
1582 (cp->tld)(sp);
1583 /* fall through */
1584 case STATE_REQ_SENT:
1585 case STATE_ACK_RCVD:
1586 case STATE_ACK_SENT:
1587 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1588 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1589 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1590 break;
1591 }
1592}
1593
1594static void
1595sppp_to_event(const struct cp *cp, struct sppp *sp)
1596{
1597 STDDCL;
1598 int s;
1599
1600 s = splimp();
1601 if (debug)
1602 log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1603 ifp->if_name, ifp->if_unit, cp->name,
1604 sppp_state_name(sp->state[cp->protoidx]),
1605 sp->rst_counter[cp->protoidx]);
1606
1607 if (--sp->rst_counter[cp->protoidx] < 0)
1608 /* TO- event */
1609 switch (sp->state[cp->protoidx]) {
1610 case STATE_CLOSING:
1611 (cp->tlf)(sp);
1612 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1613 break;
1614 case STATE_STOPPING:
1615 (cp->tlf)(sp);
1616 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1617 break;
1618 case STATE_REQ_SENT:
1619 case STATE_ACK_RCVD:
1620 case STATE_ACK_SENT:
1621 (cp->tlf)(sp);
1622 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1623 break;
1624 }
1625 else
1626 /* TO+ event */
1627 switch (sp->state[cp->protoidx]) {
1628 case STATE_CLOSING:
1629 case STATE_STOPPING:
1630 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1631 0, 0);
1632 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1633 sp->lcp.timeout);
1634 break;
1635 case STATE_REQ_SENT:
1636 case STATE_ACK_RCVD:
1637 (cp->scr)(sp);
1638 /* sppp_cp_change_state() will restart the timer */
1639 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1640 break;
1641 case STATE_ACK_SENT:
1642 (cp->scr)(sp);
1643 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1644 sp->lcp.timeout);
1645 break;
1646 }
1647
1648 splx(s);
1649}
1650
1651/*
1652 * Change the state of a control protocol in the state automaton.
1653 * Takes care of starting/stopping the restart timer.
1654 */
1655void
1656sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1657{
1658 sp->state[cp->protoidx] = newstate;
1659
1660 untimeout(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1661 switch (newstate) {
1662 case STATE_INITIAL:
1663 case STATE_STARTING:
1664 case STATE_CLOSED:
1665 case STATE_STOPPED:
1666 case STATE_OPENED:
1667 break;
1668 case STATE_CLOSING:
1669 case STATE_STOPPING:
1670 case STATE_REQ_SENT:
1671 case STATE_ACK_RCVD:
1672 case STATE_ACK_SENT:
1673 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1674 sp->lcp.timeout);
1675 break;
1676 }
1677}
1678/*
1679 *--------------------------------------------------------------------------*
1680 * *
1681 * The LCP implementation. *
1682 * *
1683 *--------------------------------------------------------------------------*
1684 */
1685static void
1686sppp_lcp_init(struct sppp *sp)
1687{
1688 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1689 sp->lcp.magic = 0;
1690 sp->state[IDX_LCP] = STATE_INITIAL;
1691 sp->fail_counter[IDX_LCP] = 0;
1692 sp->lcp.protos = 0;
1693 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1694
1695 /*
1696 * Initialize counters and timeout values. Note that we don't
1697 * use the 3 seconds suggested in RFC 1661 since we are likely
1698 * running on a fast link. XXX We should probably implement
1699 * the exponential backoff option. Note that these values are
1700 * relevant for all control protocols, not just LCP only.
1701 */
1702 sp->lcp.timeout = 1 * hz;
1703 sp->lcp.max_terminate = 2;
1704 sp->lcp.max_configure = 10;
1705 sp->lcp.max_failure = 10;
1706 callout_handle_init(&sp->ch[IDX_LCP]);
1707}
1708
1709static void
1710sppp_lcp_up(struct sppp *sp)
1711{
1712 STDDCL;
1713
1714 /*
1715 * If this interface is passive or dial-on-demand, and we are
1716 * still in Initial state, it means we've got an incoming
1717 * call. Activate the interface.
1718 */
1719 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1720 if (debug)
1721 log(LOG_DEBUG,
1722 "%s%d: Up event", ifp->if_name, ifp->if_unit);
1723 ifp->if_flags |= IFF_RUNNING;
1724 if (sp->state[IDX_LCP] == STATE_INITIAL) {
1725 if (debug)
1726 addlog("(incoming call)\n");
1727 sp->pp_flags |= PP_CALLIN;
1728 lcp.Open(sp);
1729 } else if (debug)
1730 addlog("\n");
1731 }
1732
1733 sppp_up_event(&lcp, sp);
1734}
1735
1736static void
1737sppp_lcp_down(struct sppp *sp)
1738{
1739 STDDCL;
1740
1741 sppp_down_event(&lcp, sp);
1742
1743 /*
1744 * If this is neither a dial-on-demand nor a passive
1745 * interface, simulate an ``ifconfig down'' action, so the
1746 * administrator can force a redial by another ``ifconfig
1747 * up''. XXX For leased line operation, should we immediately
1748 * try to reopen the connection here?
1749 */
1750 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1751 log(LOG_INFO,
1752 "%s%d: Down event (carrier loss), taking interface down.\n",
1753 ifp->if_name, ifp->if_unit);
1754 if_down(ifp);
1755 } else {
1756 if (debug)
1757 log(LOG_DEBUG,
1758 "%s%d: Down event (carrier loss)\n",
1759 ifp->if_name, ifp->if_unit);
1760 }
1761 sp->pp_flags &= ~PP_CALLIN;
1762 if (sp->state[IDX_LCP] != STATE_INITIAL)
1763 lcp.Close(sp);
1764 ifp->if_flags &= ~IFF_RUNNING;
1765}
1766
1767static void
1768sppp_lcp_open(struct sppp *sp)
1769{
1770 /*
1771 * If we are authenticator, negotiate LCP_AUTH
1772 */
1773 if (sp->hisauth.proto != 0)
1774 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1775 else
1776 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1777 sp->pp_flags &= ~PP_NEEDAUTH;
1778 sppp_open_event(&lcp, sp);
1779}
1780
1781static void
1782sppp_lcp_close(struct sppp *sp)
1783{
1784 sppp_close_event(&lcp, sp);
1785}
1786
1787static void
1788sppp_lcp_TO(void *cookie)
1789{
1790 sppp_to_event(&lcp, (struct sppp *)cookie);
1791}
1792
1793/*
1794 * Analyze a configure request. Return true if it was agreeable, and
1795 * caused action sca, false if it has been rejected or nak'ed, and
1796 * caused action scn. (The return value is used to make the state
1797 * transition decision in the state automaton.)
1798 */
1799static int
1800sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1801{
1802 STDDCL;
1803 u_char *buf, *r, *p;
1804 int origlen, rlen;
1805 u_long nmagic;
1806 u_short authproto;
1807
1808 len -= 4;
1809 origlen = len;
1810 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1811 if (! buf)
1812 return (0);
1813
1814 if (debug)
1815 log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1816 ifp->if_name, ifp->if_unit);
1817
1818 /* pass 1: check for things that need to be rejected */
1819 p = (void*) (h+1);
1820 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1821 if (debug)
1822 addlog(" %s ", sppp_lcp_opt_name(*p));
1823 switch (*p) {
1824 case LCP_OPT_MAGIC:
1825 /* Magic number. */
1826 /* fall through, both are same length */
1827 case LCP_OPT_ASYNC_MAP:
1828 /* Async control character map. */
1829 if (len >= 6 || p[1] == 6)
1830 continue;
1831 if (debug)
1832 addlog("[invalid] ");
1833 break;
1834 case LCP_OPT_MRU:
1835 /* Maximum receive unit. */
1836 if (len >= 4 && p[1] == 4)
1837 continue;
1838 if (debug)
1839 addlog("[invalid] ");
1840 break;
1841 case LCP_OPT_AUTH_PROTO:
1842 if (len < 4) {
1843 if (debug)
1844 addlog("[invalid] ");
1845 break;
1846 }
1847 authproto = (p[2] << 8) + p[3];
1848 if (authproto == PPP_CHAP && p[1] != 5) {
1849 if (debug)
1850 addlog("[invalid chap len] ");
1851 break;
1852 }
1853 if (sp->myauth.proto == 0) {
1854 /* we are not configured to do auth */
1855 if (debug)
1856 addlog("[not configured] ");
1857 break;
1858 }
1859 /*
1860 * Remote want us to authenticate, remember this,
1861 * so we stay in PHASE_AUTHENTICATE after LCP got
1862 * up.
1863 */
1864 sp->pp_flags |= PP_NEEDAUTH;
1865 continue;
1866 default:
1867 /* Others not supported. */
1868 if (debug)
1869 addlog("[rej] ");
1870 break;
1871 }
1872 /* Add the option to rejected list. */
1873 bcopy (p, r, p[1]);
1874 r += p[1];
1875 rlen += p[1];
1876 }
1877 if (rlen) {
1878 if (debug)
1879 addlog(" send conf-rej\n");
1880 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1881 return 0;
1882 } else if (debug)
1883 addlog("\n");
1884
1885 /*
1886 * pass 2: check for option values that are unacceptable and
1887 * thus require to be nak'ed.
1888 */
1889 if (debug)
1890 log(LOG_DEBUG, "%s%d: lcp parse opt values: ",
1891 ifp->if_name, ifp->if_unit);
1892
1893 p = (void*) (h+1);
1894 len = origlen;
1895 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1896 if (debug)
1897 addlog(" %s ", sppp_lcp_opt_name(*p));
1898 switch (*p) {
1899 case LCP_OPT_MAGIC:
1900 /* Magic number -- extract. */
1901 nmagic = (u_long)p[2] << 24 |
1902 (u_long)p[3] << 16 | p[4] << 8 | p[5];
1903 if (nmagic != sp->lcp.magic) {
1904 if (debug)
1905 addlog("0x%x ", nmagic);
1906 continue;
1907 }
1908 /*
1909 * Local and remote magics equal -- loopback?
1910 */
1911 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1912 printf ("%s%d: loopback\n",
1913 ifp->if_name, ifp->if_unit);
1914 sp->pp_loopcnt = 0;
1915 if (ifp->if_flags & IFF_UP) {
1916 if_down(ifp);
1917 sppp_qflush(&sp->pp_cpq);
1918 /* XXX ? */
1919 lcp.Down(sp);
1920 lcp.Up(sp);
1921 }
1922 } else if (debug)
1923 addlog("[glitch] ");
1924 ++sp->pp_loopcnt;
1925 /*
1926 * We negate our magic here, and NAK it. If
1927 * we see it later in an NAK packet, we
1928 * suggest a new one.
1929 */
1930 nmagic = ~sp->lcp.magic;
1931 /* Gonna NAK it. */
1932 p[2] = nmagic >> 24;
1933 p[3] = nmagic >> 16;
1934 p[4] = nmagic >> 8;
1935 p[5] = nmagic;
1936 break;
1937
1938 case LCP_OPT_ASYNC_MAP:
1939 /* Async control character map -- check to be zero. */
1940 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1941 if (debug)
1942 addlog("[empty] ");
1943 continue;
1944 }
1945 if (debug)
1946 addlog("[non-empty] ");
1947 /* suggest a zero one */
1948 p[2] = p[3] = p[4] = p[5] = 0;
1949 break;
1950
1951 case LCP_OPT_MRU:
1952 /*
1953 * Maximum receive unit. Always agreeable,
1954 * but ignored by now.
1955 */
1956 sp->lcp.their_mru = p[2] * 256 + p[3];
1957 if (debug)
1958 addlog("%d ", sp->lcp.their_mru);
1959 continue;
1960
1961 case LCP_OPT_AUTH_PROTO:
1962 authproto = (p[2] << 8) + p[3];
1963 if (sp->myauth.proto != authproto) {
1964 /* not agreed, nak */
1965 if (debug)
1966 addlog("[mine %s != his %s] ",
1967 sppp_proto_name(sp->hisauth.proto),
1968 sppp_proto_name(authproto));
1969 p[2] = sp->myauth.proto >> 8;
1970 p[3] = sp->myauth.proto;
1971 break;
1972 }
1973 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
1974 if (debug)
1975 addlog("[chap not MD5] ");
1976 p[4] == CHAP_MD5;
1977 break;
1978 }
1979 continue;
1980 }
1981 /* Add the option to nak'ed list. */
1982 bcopy (p, r, p[1]);
1983 r += p[1];
1984 rlen += p[1];
1985 }
1986 if (rlen) {
1987 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1988 if (debug)
1989 addlog(" max_failure (%d) exceeded, "
1990 "send conf-rej\n",
1991 sp->lcp.max_failure);
1992 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1993 } else {
1994 if (debug)
1995 addlog(" send conf-nak\n");
1996 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1997 }
1998 return 0;
1999 } else {
2000 if (debug)
2001 addlog(" send conf-ack\n");
2002 sp->fail_counter[IDX_LCP] = 0;
2003 sp->pp_loopcnt = 0;
2004 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2005 h->ident, origlen, h+1);
2006 }
2007
2008 free (buf, M_TEMP);
2009 return (rlen == 0);
2010}
2011
2012/*
2013 * Analyze the LCP Configure-Reject option list, and adjust our
2014 * negotiation.
2015 */
2016static void
2017sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2018{
2019 STDDCL;
2020 u_char *buf, *p;
2021
2022 len -= 4;
2023 buf = malloc (len, M_TEMP, M_NOWAIT);
2024 if (!buf)
2025 return;
2026
2027 if (debug)
2028 log(LOG_DEBUG, "%s%d: lcp rej opts: ",
2029 ifp->if_name, ifp->if_unit);
2030
2031 p = (void*) (h+1);
2032 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2033 if (debug)
2034 addlog(" %s ", sppp_lcp_opt_name(*p));
2035 switch (*p) {
2036 case LCP_OPT_MAGIC:
2037 /* Magic number -- can't use it, use 0 */
2038 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2039 sp->lcp.magic = 0;
2040 break;
2041 case LCP_OPT_MRU:
2042 /*
2043 * Should not be rejected anyway, since we only
2044 * negotiate a MRU if explicitly requested by
2045 * peer.
2046 */
2047 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2048 break;
2049 case LCP_OPT_AUTH_PROTO:
2050 /*
2051 * Peer doesn't want to authenticate himself,
2052 * deny unless this is a dialout call, and
2053 * AUTHFLAG_NOCALLOUT is set.
2054 */
2055 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2056 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2057 if (debug)
2058 addlog("[don't insist on auth "
2059 "for callout]");
2060 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2061 break;
2062 }
2063 if (debug)
2064 addlog("[access denied]\n");
2065 lcp.Close(sp);
2066 break;
2067 }
2068 }
2069 if (debug)
2070 addlog("\n");
2071 free (buf, M_TEMP);
2072 return;
2073}
2074
2075/*
2076 * Analyze the LCP Configure-NAK option list, and adjust our
2077 * negotiation.
2078 */
2079static void
2080sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2081{
2082 STDDCL;
2083 u_char *buf, *p;
2084 u_long magic;
2085
2086 len -= 4;
2087 buf = malloc (len, M_TEMP, M_NOWAIT);
2088 if (!buf)
2089 return;
2090
2091 if (debug)
2092 log(LOG_DEBUG, "%s%d: lcp nak opts: ",
2093 ifp->if_name, ifp->if_unit);
2094
2095 p = (void*) (h+1);
2096 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2097 if (debug)
2098 addlog(" %s ", sppp_lcp_opt_name(*p));
2099 switch (*p) {
2100 case LCP_OPT_MAGIC:
2101 /* Magic number -- renegotiate */
2102 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2103 len >= 6 && p[1] == 6) {
2104 magic = (u_long)p[2] << 24 |
2105 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2106 /*
2107 * If the remote magic is our negated one,
2108 * this looks like a loopback problem.
2109 * Suggest a new magic to make sure.
2110 */
2111 if (magic == ~sp->lcp.magic) {
2112 if (debug)
2113 addlog("magic glitch ");
2114 sp->lcp.magic += time.tv_sec + time.tv_usec;
2115 } else {
2116 sp->lcp.magic = magic;
2117 if (debug)
2118 addlog("%d ");
2119 }
2120 }
2121 break;
2122 case LCP_OPT_MRU:
2123 /*
2124 * Peer wants to advise us to negotiate an MRU.
2125 * Agree on it if it's reasonable, or use
2126 * default otherwise.
2127 */
2128 if (len >= 4 && p[1] == 4) {
2129 u_int mru = p[2] * 256 + p[3];
2130 if (debug)
2131 addlog("%d ", mru);
2132 if (mru < PP_MTU || mru > PP_MAX_MRU)
2133 mru = PP_MTU;
2134 sp->lcp.mru = mru;
2135 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2136 }
2137 break;
2138 case LCP_OPT_AUTH_PROTO:
2139 /*
2140 * Peer doesn't like our authentication method,
2141 * deny.
2142 */
2143 if (debug)
2144 addlog("[access denied]\n");
2145 lcp.Close(sp);
2146 break;
2147 }
2148 }
2149 if (debug)
2150 addlog("\n");
2151 free (buf, M_TEMP);
2152 return;
2153}
2154
2155static void
2156sppp_lcp_tlu(struct sppp *sp)
2157{
2158 STDDCL;
2159 int i;
2160 u_long mask;
2161
2162 /* XXX ? */
2163 if (! (ifp->if_flags & IFF_UP) &&
2164 (ifp->if_flags & IFF_RUNNING)) {
2165 /* Coming out of loopback mode. */
2166 if_up(ifp);
2167 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
2168 }
2169
2170 for (i = 0; i < IDX_COUNT; i++)
2171 if ((cps[i])->flags & CP_QUAL)
2172 (cps[i])->Open(sp);
2173
2174 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2175 (sp->pp_flags & PP_NEEDAUTH) != 0)
2176 sp->pp_phase = PHASE_AUTHENTICATE;
2177 else
2178 sp->pp_phase = PHASE_NETWORK;
2179
2180 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2181 sppp_phase_name(sp->pp_phase));
2182
2183 /*
2184 * Open all authentication protocols. This is even required
2185 * if we already proceeded to network phase, since it might be
2186 * that remote wants us to authenticate, so we might have to
2187 * send a PAP request. Undesired authentication protocols
2188 * don't do anything when they get an Open event.
2189 */
2190 for (i = 0; i < IDX_COUNT; i++)
2191 if ((cps[i])->flags & CP_AUTH)
2192 (cps[i])->Open(sp);
2193
2194 if (sp->pp_phase == PHASE_NETWORK) {
2195 /* Notify all NCPs. */
2196 for (i = 0; i < IDX_COUNT; i++)
2197 if ((cps[i])->flags & CP_NCP)
2198 (cps[i])->Open(sp);
2199 }
2200
2201 /* Send Up events to all started protos. */
2202 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2203 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2204 (cps[i])->Up(sp);
2205
2206 if (sp->pp_phase == PHASE_NETWORK)
2207 /* if no NCP is starting, close down */
2208 sppp_lcp_check_and_close(sp);
2209}
2210
2211static void
2212sppp_lcp_tld(struct sppp *sp)
2213{
2214 STDDCL;
2215 int i;
2216 u_long mask;
2217
2218 sp->pp_phase = PHASE_TERMINATE;
2219
2220 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2221 sppp_phase_name(sp->pp_phase));
2222
2223 /*
2224 * Take upper layers down. We send the Down event first and
2225 * the Close second to prevent the upper layers from sending
2226 * ``a flurry of terminate-request packets'', as the RFC
2227 * describes it.
2228 */
2229 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2230 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2231 (cps[i])->Down(sp);
2232 (cps[i])->Close(sp);
2233 }
2234}
2235
2236static void
2237sppp_lcp_tls(struct sppp *sp)
2238{
2239 STDDCL;
2240
2241 sp->pp_phase = PHASE_ESTABLISH;
2242
2243 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2244 sppp_phase_name(sp->pp_phase));
2245
2246 /* Notify lower layer if desired. */
2247 if (sp->pp_tls)
2248 (sp->pp_tls)(sp);
2249}
2250
2251static void
2252sppp_lcp_tlf(struct sppp *sp)
2253{
2254 STDDCL;
2255
2256 sp->pp_phase = PHASE_DEAD;
2257 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2258 sppp_phase_name(sp->pp_phase));
2259
2260 /* Notify lower layer if desired. */
2261 if (sp->pp_tlf)
2262 (sp->pp_tlf)(sp);
2263}
2264
2265static void
2266sppp_lcp_scr(struct sppp *sp)
2267{
2268 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2269 int i = 0;
2270 u_short authproto;
2271
2272 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2273 if (! sp->lcp.magic)
2274 sp->lcp.magic = time.tv_sec + time.tv_usec;
2275 opt[i++] = LCP_OPT_MAGIC;
2276 opt[i++] = 6;
2277 opt[i++] = sp->lcp.magic >> 24;
2278 opt[i++] = sp->lcp.magic >> 16;
2279 opt[i++] = sp->lcp.magic >> 8;
2280 opt[i++] = sp->lcp.magic;
2281 }
2282
2283 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2284 opt[i++] = LCP_OPT_MRU;
2285 opt[i++] = 4;
2286 opt[i++] = sp->lcp.mru >> 8;
2287 opt[i++] = sp->lcp.mru;
2288 }
2289
2290 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2291 authproto = sp->hisauth.proto;
2292 opt[i++] = LCP_OPT_AUTH_PROTO;
2293 opt[i++] = authproto == PPP_CHAP? 5: 4;
2294 opt[i++] = authproto >> 8;
2295 opt[i++] = authproto;
2296 if (authproto == PPP_CHAP)
2297 opt[i++] = CHAP_MD5;
2298 }
2299
2300 sp->confid[IDX_LCP] = ++sp->pp_seq;
2301 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2302}
2303
2304/*
2305 * Check the open NCPs, return true if at least one NCP is open.
2306 */
2307static int
2308sppp_ncp_check(struct sppp *sp)
2309{
2310 int i, mask;
2311
2312 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2313 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2314 return 1;
2315 return 0;
2316}
2317
2318/*
2319 * Re-check the open NCPs and see if we should terminate the link.
2320 * Called by the NCPs during their tlf action handling.
2321 */
2322static void
2323sppp_lcp_check_and_close(struct sppp *sp)
2324{
2325
2326 if (sp->pp_phase < PHASE_NETWORK)
2327 /* don't bother, we are already going down */
2328 return;
2329
2330 if (sppp_ncp_check(sp))
2331 return;
2332
2333 lcp.Close(sp);
2334}
2335/*
2336 *--------------------------------------------------------------------------*
2337 * *
2338 * The IPCP implementation. *
2339 * *
2340 *--------------------------------------------------------------------------*
2341 */
2342
2343static void
2344sppp_ipcp_init(struct sppp *sp)
2345{
2346 sp->ipcp.opts = 0;
2347 sp->ipcp.flags = 0;
2348 sp->state[IDX_IPCP] = STATE_INITIAL;
2349 sp->fail_counter[IDX_IPCP] = 0;
2350 callout_handle_init(&sp->ch[IDX_IPCP]);
2351}
2352
2353static void
2354sppp_ipcp_up(struct sppp *sp)
2355{
2356 sppp_up_event(&ipcp, sp);
2357}
2358
2359static void
2360sppp_ipcp_down(struct sppp *sp)
2361{
2362 sppp_down_event(&ipcp, sp);
2363}
2364
2365static void
2366sppp_ipcp_open(struct sppp *sp)
2367{
2368 STDDCL;
2369 u_long myaddr, hisaddr;
2370
2371 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2372 /*
2373 * If we don't have his address, this probably means our
2374 * interface doesn't want to talk IP at all. (This could
2375 * be the case if somebody wants to speak only IPX, for
2376 * example.) Don't open IPCP in this case.
2377 */
2378 if (hisaddr == 0L) {
2379 /* XXX this message should go away */
2380 if (debug)
2381 log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2382 ifp->if_name, ifp->if_unit);
2383 return;
2384 }
2385
2386 if (myaddr == 0L) {
2387 /*
2388 * I don't have an assigned address, so i need to
2389 * negotiate my address.
2390 */
2391 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2392 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2393 }
2394 sppp_open_event(&ipcp, sp);
2395}
2396
2397static void
2398sppp_ipcp_close(struct sppp *sp)
2399{
2400 sppp_close_event(&ipcp, sp);
2401 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2402 /*
2403 * My address was dynamic, clear it again.
2404 */
2405 sppp_set_ip_addr(sp, 0L);
2406}
2407
2408static void
2409sppp_ipcp_TO(void *cookie)
2410{
2411 sppp_to_event(&ipcp, (struct sppp *)cookie);
2412}
2413
2414/*
2415 * Analyze a configure request. Return true if it was agreeable, and
2416 * caused action sca, false if it has been rejected or nak'ed, and
2417 * caused action scn. (The return value is used to make the state
2418 * transition decision in the state automaton.)
2419 */
2420static int
2421sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2422{
2423 u_char *buf, *r, *p;
2424 struct ifnet *ifp = &sp->pp_if;
2425 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2426 u_long hisaddr, desiredaddr;
2427
2428 len -= 4;
2429 origlen = len;
2430 /*
2431 * Make sure to allocate a buf that can at least hold a
2432 * conf-nak with an `address' option. We might need it below.
2433 */
2434 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2435 if (! buf)
2436 return (0);
2437
2438 /* pass 1: see if we can recognize them */
2439 if (debug)
2440 log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2441 ifp->if_name, ifp->if_unit);
2442 p = (void*) (h+1);
2443 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2444 if (debug)
2445 addlog(" %s ", sppp_ipcp_opt_name(*p));
2446 switch (*p) {
2447#ifdef notyet
2448 case IPCP_OPT_COMPRESSION:
2449 if (len >= 6 && p[1] >= 6) {
2450 /* correctly formed compress option */
2451 continue;
2452 }
2453 if (debug)
2454 addlog("[invalid] ");
2455 break;
2456#endif
2457 case IPCP_OPT_ADDRESS:
2458 if (len >= 6 && p[1] == 6) {
2459 /* correctly formed address option */
2460 continue;
2461 }
2462 if (debug)
2463 addlog("[invalid] ");
2464 break;
2465 default:
2466 /* Others not supported. */
2467 if (debug)
2468 addlog("[rej] ");
2469 break;
2470 }
2471 /* Add the option to rejected list. */
2472 bcopy (p, r, p[1]);
2473 r += p[1];
2474 rlen += p[1];
2475 }
2476 if (rlen) {
2477 if (debug)
2478 addlog(" send conf-rej\n");
2479 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2480 return 0;
2481 } else if (debug)
2482 addlog("\n");
2483
2484 /* pass 2: parse option values */
2485 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2486 if (debug)
2487 log(LOG_DEBUG, "%s%d: ipcp parse opt values: ",
2488 ifp->if_name, ifp->if_unit);
2489 p = (void*) (h+1);
2490 len = origlen;
2491 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2492 if (debug)
2493 addlog(" %s ", sppp_ipcp_opt_name(*p));
2494 switch (*p) {
2495#ifdef notyet
2496 case IPCP_OPT_COMPRESSION:
2497 continue;
2498#endif
2499 case IPCP_OPT_ADDRESS:
2500 desiredaddr = p[2] << 24 | p[3] << 16 |
2501 p[4] << 8 | p[5];
2502 if (desiredaddr == hisaddr) {
2503 /*
2504 * Peer's address is same as our value,
2505 * this is agreeable. Gonna conf-ack
2506 * it.
2507 */
2508 if (debug)
2509 addlog("%s [ack] ",
2510 sppp_dotted_quad(hisaddr));
2511 /* record that we've seen it already */
2512 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2513 continue;
2514 }
2515 /*
2516 * The address wasn't agreeable. This is either
2517 * he sent us 0.0.0.0, asking to assign him an
2518 * address, or he send us another address not
2519 * matching our value. Either case, we gonna
2520 * conf-nak it with our value.
2521 */
2522 if (debug) {
2523 if (desiredaddr == 0)
2524 addlog("[addr requested] ");
2525 else
2526 addlog("%s [not agreed] ",
2527 sppp_dotted_quad(desiredaddr));
2528
2529 p[2] = hisaddr >> 24;
2530 p[3] = hisaddr >> 16;
2531 p[4] = hisaddr >> 8;
2532 p[5] = hisaddr;
2533 }
2534 break;
2535 }
2536 /* Add the option to nak'ed list. */
2537 bcopy (p, r, p[1]);
2538 r += p[1];
2539 rlen += p[1];
2540 }
2541
2542 /*
2543 * If we are about to conf-ack the request, but haven't seen
2544 * his address so far, gonna conf-nak it instead, with the
2545 * `address' option present and our idea of his address being
2546 * filled in there, to request negotiation of both addresses.
2547 *
2548 * XXX This can result in an endless req - nak loop if peer
2549 * doesn't want to send us his address. Q: What should we do
2550 * about it? XXX A: implement the max-failure counter.
2551 */
2552 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2553 buf[0] = IPCP_OPT_ADDRESS;
2554 buf[1] = 6;
2555 buf[2] = hisaddr >> 24;
2556 buf[3] = hisaddr >> 16;
2557 buf[4] = hisaddr >> 8;
2558 buf[5] = hisaddr;
2559 rlen = 6;
2560 if (debug)
2561 addlog("still need hisaddr ");
2562 }
2563
2564 if (rlen) {
2565 if (debug)
2566 addlog(" send conf-nak\n");
2567 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2568 } else {
2569 if (debug)
2570 addlog(" send conf-ack\n");
2571 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2572 h->ident, origlen, h+1);
2573 }
2574
2575 free (buf, M_TEMP);
2576 return (rlen == 0);
2577}
2578
2579/*
2580 * Analyze the IPCP Configure-Reject option list, and adjust our
2581 * negotiation.
2582 */
2583static void
2584sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2585{
2586 u_char *buf, *p;
2587 struct ifnet *ifp = &sp->pp_if;
2588 int debug = ifp->if_flags & IFF_DEBUG;
2589
2590 len -= 4;
2591 buf = malloc (len, M_TEMP, M_NOWAIT);
2592 if (!buf)
2593 return;
2594
2595 if (debug)
2596 log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2597 ifp->if_name, ifp->if_unit);
2598
2599 p = (void*) (h+1);
2600 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2601 if (debug)
2602 addlog(" %s ", sppp_ipcp_opt_name(*p));
2603 switch (*p) {
2604 case IPCP_OPT_ADDRESS:
2605 /*
2606 * Peer doesn't grok address option. This is
2607 * bad. XXX Should we better give up here?
2608 */
2609 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2610 break;
2611#ifdef notyet
2612 case IPCP_OPT_COMPRESS:
2613 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2614 break;
2615#endif
2616 }
2617 }
2618 if (debug)
2619 addlog("\n");
2620 free (buf, M_TEMP);
2621 return;
2622}
2623
2624/*
2625 * Analyze the IPCP Configure-NAK option list, and adjust our
2626 * negotiation.
2627 */
2628static void
2629sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2630{
2631 u_char *buf, *p;
2632 struct ifnet *ifp = &sp->pp_if;
2633 int debug = ifp->if_flags & IFF_DEBUG;
2634 u_long wantaddr;
2635
2636 len -= 4;
2637 buf = malloc (len, M_TEMP, M_NOWAIT);
2638 if (!buf)
2639 return;
2640
2641 if (debug)
2642 log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2643 ifp->if_name, ifp->if_unit);
2644
2645 p = (void*) (h+1);
2646 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2647 if (debug)
2648 addlog(" %s ", sppp_ipcp_opt_name(*p));
2649 switch (*p) {
2650 case IPCP_OPT_ADDRESS:
2651 /*
2652 * Peer doesn't like our local IP address. See
2653 * if we can do something for him. We'll drop
2654 * him our address then.
2655 */
2656 if (len >= 6 && p[1] == 6) {
2657 wantaddr = p[2] << 24 | p[3] << 16 |
2658 p[4] << 8 | p[5];
2659 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2660 if (debug)
2661 addlog("[wantaddr %s] ",
2662 sppp_dotted_quad(wantaddr));
2663 /*
2664 * When doing dynamic address assignment,
2665 * we accept his offer. Otherwise, we
2666 * ignore it and thus continue to negotiate
2667 * our already existing value.
2668 */
2669 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2670 sppp_set_ip_addr(sp, wantaddr);
2671 if (debug)
2672 addlog("[agree] ");
2673 }
2674 }
2675 break;
2676#ifdef notyet
2677 case IPCP_OPT_COMPRESS:
2678 /*
2679 * Peer wants different compression parameters.
2680 */
2681 break;
2682#endif
2683 }
2684 }
2685 if (debug)
2686 addlog("\n");
2687 free (buf, M_TEMP);
2688 return;
2689}
2690
2691static void
2692sppp_ipcp_tlu(struct sppp *sp)
2693{
2694}
2695
2696static void
2697sppp_ipcp_tld(struct sppp *sp)
2698{
2699}
2700
2701static void
2702sppp_ipcp_tls(struct sppp *sp)
2703{
2704 /* indicate to LCP that it must stay alive */
2705 sp->lcp.protos |= (1 << IDX_IPCP);
2706}
2707
2708static void
2709sppp_ipcp_tlf(struct sppp *sp)
2710{
2711 /* we no longer need LCP */
2712 sp->lcp.protos &= ~(1 << IDX_IPCP);
2713 sppp_lcp_check_and_close(sp);
2714}
2715
2716static void
2717sppp_ipcp_scr(struct sppp *sp)
2718{
2719 char opt[6 /* compression */ + 6 /* address */];
2720 u_long ouraddr;
2721 int i = 0;
2722
2723#ifdef notyet
2724 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2725 opt[i++] = IPCP_OPT_COMPRESSION;
2726 opt[i++] = 6;
2727 opt[i++] = 0; /* VJ header compression */
2728 opt[i++] = 0x2d; /* VJ header compression */
2729 opt[i++] = max_slot_id;
2730 opt[i++] = comp_slot_id;
2731 }
2732#endif
2733
2734 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2735 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2736 opt[i++] = IPCP_OPT_ADDRESS;
2737 opt[i++] = 6;
2738 opt[i++] = ouraddr >> 24;
2739 opt[i++] = ouraddr >> 16;
2740 opt[i++] = ouraddr >> 8;
2741 opt[i++] = ouraddr;
2742 }
2743
2744 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2745 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2746}
2747
2748
2749/*
2750 *--------------------------------------------------------------------------*
2751 * *
2752 * The CHAP implementation. *
2753 * *
2754 *--------------------------------------------------------------------------*
2755 */
2756
2757/*
2758 * The authentication protocols don't employ a full-fledged state machine as
2759 * the control protocols do, since they do have Open and Close events, but
2760 * not Up and Down, nor are they explicitly terminated. Also, use of the
2761 * authentication protocols may be different in both directions (this makes
2762 * sense, think of a machine that never accepts incoming calls but only
2763 * calls out, it doesn't require the called party to authenticate itself).
2764 *
2765 * Our state machine for the local authentication protocol (we are requesting
2766 * the peer to authenticate) looks like:
2767 *
2768 * RCA-
2769 * +--------------------------------------------+
2770 * V scn,tld|
2771 * +--------+ Close +---------+ RCA+
2772 * | |<----------------------------------| |------+
2773 * +--->| Closed | TO* | Opened | sca |
2774 * | | |-----+ +-------| |<-----+
2775 * | +--------+ irc | | +---------+
2776 * | ^ | | ^
2777 * | | | | |
2778 * | | | | |
2779 * | TO-| | | |
2780 * | |tld TO+ V | |
2781 * | | +------->+ | |
2782 * | | | | | |
2783 * | +--------+ V | |
2784 * | | |<----+<--------------------+ |
2785 * | | Req- | scr |
2786 * | | Sent | |
2787 * | | | |
2788 * | +--------+ |
2789 * | RCA- | | RCA+ |
2790 * +------+ +------------------------------------------+
2791 * scn,tld sca,irc,ict,tlu
2792 *
2793 *
2794 * with:
2795 *
2796 * Open: LCP reached authentication phase
2797 * Close: LCP reached terminate phase
2798 *
2799 * RCA+: received reply (pap-req, chap-response), acceptable
2800 * RCN: received reply (pap-req, chap-response), not acceptable
2801 * TO+: timeout with restart counter >= 0
2802 * TO-: timeout with restart counter < 0
2803 * TO*: reschedule timeout for CHAP
2804 *
2805 * scr: send request packet (none for PAP, chap-challenge)
2806 * sca: send ack packet (pap-ack, chap-success)
2807 * scn: send nak packet (pap-nak, chap-failure)
2808 * ict: initialize re-challenge timer (CHAP only)
2809 *
2810 * tlu: this-layer-up, LCP reaches network phase
2811 * tld: this-layer-down, LCP enters terminate phase
2812 *
2813 * Note that in CHAP mode, after sending a new challenge, while the state
2814 * automaton falls back into Req-Sent state, it doesn't signal a tld
2815 * event to LCP, so LCP remains in network phase. Only after not getting
2816 * any response (or after getting an unacceptable response), CHAP closes,
2817 * causing LCP to enter terminate phase.
2818 *
2819 * With PAP, there is no initial request that can be sent. The peer is
2820 * expected to send one based on the successful negotiation of PAP as
2821 * the authentication protocol during the LCP option negotiation.
2822 *
2823 * Incoming authentication protocol requests (remote requests
2824 * authentication, we are peer) don't employ a state machine at all,
2825 * they are simply answered. Some peers [Ascend P50 firmware rev
2826 * 4.50] react allergically when sending IPCP requests while they are
2827 * still in authentication phase (thereby violating the standard that
2828 * demands that these NCP packets are to be discarded), so we keep
2829 * track of the peer demanding us to authenticate, and only proceed to
2830 * phase network once we've seen a positive acknowledge for the
2831 * authentication.
2832 */
2833
2834/*
2835 * Handle incoming CHAP packets.
2836 */
2837void
2838sppp_chap_input(struct sppp *sp, struct mbuf *m)
2839{
2840 STDDCL;
2841 struct lcp_header *h;
2842 int len, x;
2843 u_char *value, *name, digest[AUTHKEYLEN], dsize;
2844 int value_len, name_len;
2845 MD5_CTX ctx;
2846
2847 len = m->m_pkthdr.len;
2848 if (len < 4) {
2849 if (debug)
2850 log(LOG_DEBUG,
2851 "%s%d: chap invalid packet length: %d bytes\n",
2852 ifp->if_name, ifp->if_unit, len);
2853 return;
2854 }
2855 h = mtod (m, struct lcp_header*);
2856 if (len > ntohs (h->len))
2857 len = ntohs (h->len);
2858
2859 switch (h->type) {
2860 /* challenge, failure and success are his authproto */
2861 case CHAP_CHALLENGE:
2862 value = 1 + (u_char*)(h+1);
2863 value_len = value[-1];
2864 name = value + value_len;
2865 name_len = len - value_len - 5;
2866 if (name_len < 0) {
2867 if (debug) {
2868 log(LOG_DEBUG,
2869 "%s%d: chap corrupted challenge "
2870 "<%s id=0x%x len=%d",
2871 ifp->if_name, ifp->if_unit,
2872 sppp_auth_type_name(PPP_CHAP, h->type),
2873 h->ident, ntohs(h->len));
2874 if (len > 4)
2875 sppp_print_bytes((u_char*) (h+1), len-4);
2876 addlog(">\n");
2877 }
2878 break;
2879 }
2880
2881 if (debug) {
2882 log(LOG_DEBUG,
2883 "%s%d: chap input <%s id=0x%x len=%d name=",
2884 ifp->if_name, ifp->if_unit,
2885 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
2886 ntohs(h->len));
2887 sppp_print_string((char*) name, name_len);
2888 addlog(" value-size=%d value=", value_len);
2889 sppp_print_bytes(value, value_len);
2890 addlog(">\n");
2891 }
2892
2893 /* Compute reply value. */
2894 MD5Init(&ctx);
2895 MD5Update(&ctx, &h->ident, 1);
2896 MD5Update(&ctx, sp->myauth.secret,
2897 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
2898 MD5Update(&ctx, value, value_len);
2899 MD5Final(digest, &ctx);
2900 dsize = sizeof digest;
2901
2902 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
2903 sizeof dsize, (const char *)&dsize,
2904 sizeof digest, digest,
2905 sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
2906 sp->myauth.name,
2907 0);
2908 break;
2909
2910 case CHAP_SUCCESS:
2911 if (debug) {
2912 log(LOG_DEBUG, "%s%d: chap success",
2913 ifp->if_name, ifp->if_unit);
2914 if (len > 4) {
2915 addlog(": ");
2916 sppp_print_string((char*)(h + 1), len - 4);
2917 }
2918 addlog("\n");
2919 }
2920 x = splimp();
2921 sp->pp_flags &= ~PP_NEEDAUTH;
2922 if (sp->myauth.proto == PPP_CHAP &&
2923 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
2924 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
2925 /*
2926 * We are authenticator for CHAP but didn't
2927 * complete yet. Leave it to tlu to proceed
2928 * to network phase.
2929 */
2930 splx(x);
2931 break;
2932 }
2933 splx(x);
2934 sppp_phase_network(sp);
2935 break;
2936
2937 case CHAP_FAILURE:
2938 if (debug) {
2939 log(LOG_INFO, "%s%d: chap failure",
2940 ifp->if_name, ifp->if_unit);
2941 if (len > 4) {
2942 addlog(": ");
2943 sppp_print_string((char*)(h + 1), len - 4);
2944 }
2945 addlog("\n");
2946 } else
2947 log(LOG_INFO, "%s%d: chap failure\n",
2948 ifp->if_name, ifp->if_unit);
2949 /* await LCP shutdown by authenticator */
2950 break;
2951
2952 /* response is my authproto */
2953 case CHAP_RESPONSE:
2954 value = 1 + (u_char*)(h+1);
2955 value_len = value[-1];
2956 name = value + value_len;
2957 name_len = len - value_len - 5;
2958 if (name_len < 0) {
2959 if (debug) {
2960 log(LOG_DEBUG,
2961 "%s%d: chap corrupted response "
2962 "<%s id=0x%x len=%d",
2963 ifp->if_name, ifp->if_unit,
2964 sppp_auth_type_name(PPP_CHAP, h->type),
2965 h->ident, ntohs(h->len));
2966 if (len > 4)
2967 sppp_print_bytes((u_char*)(h+1), len-4);
2968 addlog(">\n");
2969 }
2970 break;
2971 }
2972 if (h->ident != sp->confid[IDX_CHAP]) {
2973 if (debug)
2974 log(LOG_DEBUG,
2975 "%s%d: chap dropping response for old ID "
2976 "(got %d, expected %d)\n",
2977 h->ident, sp->confid[IDX_CHAP]);
2978 break;
2979 }
2980 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
2981 || bcmp(name, sp->hisauth.name, name_len) != 0) {
2982 log(LOG_INFO, "%s%d: chap response, his name ",
2983 ifp->if_name, ifp->if_unit);
2984 sppp_print_string(name, name_len);
2985 addlog(" != expected ");
2986 sppp_print_string(sp->hisauth.name,
2987 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
2988 addlog("\n");
2989 }
2990 if (debug) {
2991 log(LOG_DEBUG, "%s%d: chap input(%s) "
2992 "<%s id=0x%x len=%d name=",
2993 ifp->if_name, ifp->if_unit,
2994 sppp_state_name(sp->state[IDX_CHAP]),
2995 sppp_auth_type_name(PPP_CHAP, h->type),
2996 h->ident, ntohs (h->len));
2997 sppp_print_string((char*)name, name_len);
2998 addlog(" value-size=%d value=", value_len);
2999 sppp_print_bytes(value, value_len);
3000 addlog(">\n");
3001 }
3002 if (value_len != AUTHKEYLEN) {
3003 if (debug)
3004 log(LOG_DEBUG,
3005 "%s%d: chap bad hash value length: "
3006 "%d bytes, should be %d\n",
3007 ifp->if_name, ifp->if_unit, value_len,
3008 AUTHKEYLEN);
3009 break;
3010 }
3011
3012 MD5Init(&ctx);
3013 MD5Update(&ctx, &h->ident, 1);
3014 MD5Update(&ctx, sp->hisauth.secret,
3015 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3016 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3017 MD5Final(digest, &ctx);
3018
3019#define FAILMSG "Failed..."
3020#define SUCCMSG "Welcome!"
3021
3022 if (value_len != sizeof digest ||
3023 bcmp(digest, value, value_len) != 0) {
3024 /* action scn, tld */
3025 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3026 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3027 0);
3028 chap.tld(sp);
3029 break;
3030 }
3031 /* action sca, perhaps tlu */
3032 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3033 sp->state[IDX_CHAP] == STATE_OPENED)
3034 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3035 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3036 0);
3037 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3038 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3039 chap.tlu(sp);
3040 }
3041 break;
3042
3043 default:
3044 /* Unknown CHAP packet type -- ignore. */
3045 if (debug) {
3046 log(LOG_DEBUG, "%s%d: chap unknown input(%s) "
3047 "<0x%x id=0x%xh len=%d",
3048 ifp->if_name, ifp->if_unit,
3049 sppp_state_name(sp->state[IDX_CHAP]),
3050 h->type, h->ident, ntohs(h->len));
3051 if (len > 4)
3052 sppp_print_bytes((u_char*)(h+1), len-4);
3053 addlog(">\n");
3054 }
3055 break;
3056
3057 }
3058}
3059
3060static void
3061sppp_chap_init(struct sppp *sp)
3062{
3063 /* Chap doesn't have STATE_INITIAL at all. */
3064 sp->state[IDX_CHAP] = STATE_CLOSED;
3065 sp->fail_counter[IDX_CHAP] = 0;
3066 callout_handle_init(&sp->ch[IDX_CHAP]);
3067}
3068
3069static void
3070sppp_chap_open(struct sppp *sp)
3071{
3072 if (sp->myauth.proto == PPP_CHAP &&
3073 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3074 /* we are authenticator for CHAP, start it */
3075 chap.scr(sp);
3076 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3077 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3078 }
3079 /* nothing to be done if we are peer, await a challenge */
3080}
3081
3082static void
3083sppp_chap_close(struct sppp *sp)
3084{
3085 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3086 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3087}
3088
3089static void
3090sppp_chap_TO(void *cookie)
3091{
3092 struct sppp *sp = (struct sppp *)cookie;
3093 STDDCL;
3094 int s;
3095
3096 s = splimp();
3097 if (debug)
3098 log(LOG_DEBUG, "%s%d: chap TO(%s) rst_counter = %d\n",
3099 ifp->if_name, ifp->if_unit,
3100 sppp_state_name(sp->state[IDX_CHAP]),
3101 sp->rst_counter[IDX_CHAP]);
3102
3103 if (--sp->rst_counter[IDX_CHAP] < 0)
3104 /* TO- event */
3105 switch (sp->state[IDX_CHAP]) {
3106 case STATE_REQ_SENT:
3107 chap.tld(sp);
3108 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3109 break;
3110 }
3111 else
3112 /* TO+ (or TO*) event */
3113 switch (sp->state[IDX_CHAP]) {
3114 case STATE_OPENED:
3115 /* TO* event */
3116 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3117 /* fall through */
3118 case STATE_REQ_SENT:
3119 chap.scr(sp);
3120 /* sppp_cp_change_state() will restart the timer */
3121 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3122 break;
3123 }
3124
3125 splx(s);
3126}
3127
3128static void
3129sppp_chap_tlu(struct sppp *sp)
3130{
3131 STDDCL;
3132 int i, x;
3133
3134 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3135
3136 /*
3137 * Some broken CHAP implementations (Conware CoNet, firmware
3138 * 4.0.?) don't want to re-authenticate their CHAP once the
3139 * initial challenge-response exchange has taken place.
3140 * Provide for an option to avoid rechallenges.
3141 */
3142 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3143 /*
3144 * Compute the re-challenge timeout. This will yield
3145 * a number between 300 and 810 seconds.
3146 */
3147 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3148
3149 sp->ch[IDX_CHAP] = timeout(chap.TO, (void *)sp, i * hz);
3150 }
3151
3152 if (debug) {
3153 log(LOG_DEBUG,
3154 "%s%d: chap %s, ",
3155 ifp->if_name, ifp->if_unit,
3156 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3157 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3158 addlog("next re-challenge in %d seconds\n", i);
3159 else
3160 addlog("re-challenging supressed\n");
3161 }
3162
3163 x = splimp();
3164 /* indicate to LCP that we need to be closed down */
3165 sp->lcp.protos |= (1 << IDX_CHAP);
3166
3167 if (sp->pp_flags & PP_NEEDAUTH) {
3168 /*
3169 * Remote is authenticator, but his auth proto didn't
3170 * complete yet. Defer the transition to network
3171 * phase.
3172 */
3173 splx(x);
3174 return;
3175 }
3176 splx(x);
3177
3178 /*
3179 * If we are already in phase network, we are done here. This
3180 * is the case if this is a dummy tlu event after a re-challenge.
3181 */
3182 if (sp->pp_phase != PHASE_NETWORK)
3183 sppp_phase_network(sp);
3184}
3185
3186static void
3187sppp_chap_tld(struct sppp *sp)
3188{
3189 STDDCL;
3190
3191 if (debug)
3192 log(LOG_DEBUG, "%s%d: chap tld\n", ifp->if_name, ifp->if_unit);
3193 untimeout(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3194 sp->lcp.protos &= ~(1 << IDX_CHAP);
3195
3196 lcp.Close(sp);
3197}
3198
3199static void
3200sppp_chap_scr(struct sppp *sp)
3201{
3202 struct timeval tv;
3203 u_long *ch, seed;
3204 u_char clen;
3205
3206 /* Compute random challenge. */
3207 ch = (u_long *)sp->myauth.challenge;
3208 microtime(&tv);
3209 seed = tv.tv_sec ^ tv.tv_usec;
3210 ch[0] = seed ^ random();
3211 ch[1] = seed ^ random();
3212 ch[2] = seed ^ random();
3213 ch[3] = seed ^ random();
3214 clen = AUTHKEYLEN;
3215
3216 sp->confid[IDX_CHAP] = ++sp->pp_seq;
3217
3218 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3219 sizeof clen, (const char *)&clen,
3220 AUTHKEYLEN, sp->myauth.challenge,
3221 sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3222 sp->myauth.name,
3223 0);
3224}
3225/*
3226 *--------------------------------------------------------------------------*
3227 * *
3228 * The PAP implementation. *
3229 * *
3230 *--------------------------------------------------------------------------*
3231 */
3232/*
3233 * For PAP, we need to keep a little state also if we are the peer, not the
3234 * authenticator. This is since we don't get a request to authenticate, but
3235 * have to repeatedly authenticate ourself until we got a response (or the
3236 * retry counter is expired).
3237 */
3238
3239/*
3240 * Handle incoming PAP packets. */
3241static void
3242sppp_pap_input(struct sppp *sp, struct mbuf *m)
3243{
3244 STDDCL;
3245 struct lcp_header *h;
3246 int len, x;
3247 u_char *name, *passwd, mlen;
3248 int name_len, passwd_len;
3249
3250 len = m->m_pkthdr.len;
3251 if (len < 5) {
3252 if (debug)
3253 log(LOG_DEBUG,
3254 "%s%d: pap invalid packet length: %d bytes\n",
3255 ifp->if_name, ifp->if_unit, len);
3256 return;
3257 }
3258 h = mtod (m, struct lcp_header*);
3259 if (len > ntohs (h->len))
3260 len = ntohs (h->len);
3261 switch (h->type) {
3262 /* PAP request is my authproto */
3263 case PAP_REQ:
3264 name = 1 + (u_char*)(h+1);
3265 name_len = name[-1];
3266 passwd = name + name_len + 1;
3267 if (name_len > len - 6 ||
3268 (passwd_len = passwd[-1]) > len - 6 - name_len) {
3269 if (debug) {
3270 log(LOG_DEBUG, "%s%d: pap corrupted input "
3271 "<%s id=0x%x len=%d",
3272 ifp->if_name, ifp->if_unit,
3273 sppp_auth_type_name(PPP_PAP, h->type),
3274 h->ident, ntohs(h->len));
3275 if (len > 4)
3276 sppp_print_bytes((u_char*)(h+1), len-4);
3277 addlog(">\n");
3278 }
3279 break;
3280 }
3281 if (debug) {
3282 log(LOG_DEBUG, "%s%d: pap input(%s) "
3283 "<%s id=0x%x len=%d name=",
3284 ifp->if_name, ifp->if_unit,
3285 sppp_state_name(sp->state[IDX_PAP]),
3286 sppp_auth_type_name(PPP_PAP, h->type),
3287 h->ident, ntohs(h->len));
3288 sppp_print_string((char*)name, name_len);
3289 addlog(" passwd=");
3290 sppp_print_string((char*)passwd, passwd_len);
3291 addlog(">\n");
3292 }
3293 if (name_len > AUTHNAMELEN ||
3294 passwd_len > AUTHKEYLEN ||
3295 bcmp(name, sp->hisauth.name, name_len) != 0 ||
3296 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3297 /* action scn, tld */
3298 mlen = sizeof(FAILMSG) - 1;
3299 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3300 sizeof mlen, (const char *)&mlen,
3301 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3302 0);
3303 pap.tld(sp);
3304 break;
3305 }
3306 /* action sca, perhaps tlu */
3307 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3308 sp->state[IDX_PAP] == STATE_OPENED) {
3309 mlen = sizeof(SUCCMSG) - 1;
3310 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3311 sizeof mlen, (const char *)&mlen,
3312 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3313 0);
3314 }
3315 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3316 sppp_cp_change_state(&pap, sp, STATE_OPENED);
3317 pap.tlu(sp);
3318 }
3319 break;
3320
3321 /* ack and nak are his authproto */
3322 case PAP_ACK:
3323 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3324 if (debug) {
3325 log(LOG_DEBUG, "%s%d: pap success",
3326 ifp->if_name, ifp->if_unit);
3327 name_len = *((char *)h);
3328 if (len > 5 && name_len) {
3329 addlog(": ");
3330 sppp_print_string((char*)(h+1), name_len);
3331 }
3332 addlog("\n");
3333 }
3334 x = splimp();
3335 sp->pp_flags &= ~PP_NEEDAUTH;
3336 if (sp->myauth.proto == PPP_PAP &&
3337 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3338 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3339 /*
3340 * We are authenticator for PAP but didn't
3341 * complete yet. Leave it to tlu to proceed
3342 * to network phase.
3343 */
3344 splx(x);
3345 break;
3346 }
3347 splx(x);
3348 sppp_phase_network(sp);
3349 break;
3350
3351 case PAP_NAK:
3352 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3353 if (debug) {
3354 log(LOG_INFO, "%s%d: pap failure",
3355 ifp->if_name, ifp->if_unit);
3356 name_len = *((char *)h);
3357 if (len > 5 && name_len) {
3358 addlog(": ");
3359 sppp_print_string((char*)(h+1), name_len);
3360 }
3361 addlog("\n");
3362 } else
3363 log(LOG_INFO, "%s%d: pap failure\n",
3364 ifp->if_name, ifp->if_unit);
3365 /* await LCP shutdown by authenticator */
3366 break;
3367
3368 default:
3369 /* Unknown PAP packet type -- ignore. */
3370 if (debug) {
3371 log(LOG_DEBUG, "%s%d: pap corrupted input "
3372 "<0x%x id=0x%x len=%d",
3373 ifp->if_name, ifp->if_unit,
3374 h->type, h->ident, ntohs(h->len));
3375 if (len > 4)
3376 sppp_print_bytes((u_char*)(h+1), len-4);
3377 addlog(">\n");
3378 }
3379 break;
3380
3381 }
3382}
3383
3384static void
3385sppp_pap_init(struct sppp *sp)
3386{
3387 /* PAP doesn't have STATE_INITIAL at all. */
3388 sp->state[IDX_PAP] = STATE_CLOSED;
3389 sp->fail_counter[IDX_PAP] = 0;
3390 callout_handle_init(&sp->ch[IDX_PAP]);
3391 callout_handle_init(&sp->pap_my_to_ch);
3392}
3393
3394static void
3395sppp_pap_open(struct sppp *sp)
3396{
3397 if (sp->hisauth.proto == PPP_PAP &&
3398 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3399 /* we are authenticator for PAP, start our timer */
3400 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3401 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3402 }
3403 if (sp->myauth.proto == PPP_PAP) {
3404 /* we are peer, send a request, and start a timer */
3405 pap.scr(sp);
3406 sp->pap_my_to_ch = timeout(sppp_pap_my_TO, (void *)sp,
3407 sp->lcp.timeout);
3408 }
3409}
3410
3411static void
3412sppp_pap_close(struct sppp *sp)
3413{
3414 if (sp->state[IDX_PAP] != STATE_CLOSED)
3415 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3416}
3417
3418/*
3419 * That's the timeout routine if we are authenticator. Since the
3420 * authenticator is basically passive in PAP, we can't do much here.
3421 */
3422static void
3423sppp_pap_TO(void *cookie)
3424{
3425 struct sppp *sp = (struct sppp *)cookie;
3426 STDDCL;
3427 int s;
3428
3429 s = splimp();
3430 if (debug)
3431 log(LOG_DEBUG, "%s%d: pap TO(%s) rst_counter = %d\n",
3432 ifp->if_name, ifp->if_unit,
3433 sppp_state_name(sp->state[IDX_PAP]),
3434 sp->rst_counter[IDX_PAP]);
3435
3436 if (--sp->rst_counter[IDX_PAP] < 0)
3437 /* TO- event */
3438 switch (sp->state[IDX_PAP]) {
3439 case STATE_REQ_SENT:
3440 pap.tld(sp);
3441 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3442 break;
3443 }
3444 else
3445 /* TO+ event, not very much we could do */
3446 switch (sp->state[IDX_PAP]) {
3447 case STATE_REQ_SENT:
3448 /* sppp_cp_change_state() will restart the timer */
3449 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3450 break;
3451 }
3452
3453 splx(s);
3454}
3455
3456/*
3457 * That's the timeout handler if we are peer. Since the peer is active,
3458 * we need to retransmit our PAP request since it is apparently lost.
3459 * XXX We should impose a max counter.
3460 */
3461static void
3462sppp_pap_my_TO(void *cookie)
3463{
3464 struct sppp *sp = (struct sppp *)cookie;
3465 STDDCL;
3466
3467 if (debug)
3468 log(LOG_DEBUG, "%s%d: pap peer TO\n",
3469 ifp->if_name, ifp->if_unit);
3470
3471 pap.scr(sp);
3472}
3473
3474static void
3475sppp_pap_tlu(struct sppp *sp)
3476{
3477 STDDCL;
3478 int x;
3479
3480 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3481
3482 if (debug)
3483 log(LOG_DEBUG, "%s%d: %s tlu\n",
3484 ifp->if_name, ifp->if_unit, pap.name);
3485
3486 x = splimp();
3487 /* indicate to LCP that we need to be closed down */
3488 sp->lcp.protos |= (1 << IDX_PAP);
3489
3490 if (sp->pp_flags & PP_NEEDAUTH) {
3491 /*
3492 * Remote is authenticator, but his auth proto didn't
3493 * complete yet. Defer the transition to network
3494 * phase.
3495 */
3496 splx(x);
3497 return;
3498 }
3499 splx(x);
3500 sppp_phase_network(sp);
3501}
3502
3503static void
3504sppp_pap_tld(struct sppp *sp)
3505{
3506 STDDCL;
3507
3508 if (debug)
3509 log(LOG_DEBUG, "%s%d: pap tld\n", ifp->if_name, ifp->if_unit);
3510 untimeout(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3511 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3512 sp->lcp.protos &= ~(1 << IDX_PAP);
3513
3514 lcp.Close(sp);
3515}
3516
3517static void
3518sppp_pap_scr(struct sppp *sp)
3519{
3520 STDDCL;
3521 u_char idlen, pwdlen;
3522
3523 sp->confid[IDX_PAP] = ++sp->pp_seq;
3524 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3525 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3526
3527 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3528 sizeof idlen, (const char *)&idlen,
3529 (unsigned)idlen, sp->myauth.name,
3530 sizeof pwdlen, (const char *)&pwdlen,
3531 (unsigned)pwdlen, sp->myauth.secret,
3532 0);
3533}
3534/*
3535 * Random miscellaneous functions.
3536 */
3537
3538/*
3539 * Send a PAP or CHAP proto packet.
3540 *
3541 * Varadic function, each of the elements for the ellipsis is of type
3542 * ``unsigned mlen, const u_char *msg''. Processing will stop iff
3543 * mlen == 0.
3544 */
3545
3546static void
3547sppp_auth_send(const struct cp *cp, struct sppp *sp, u_char type, u_char id,
3548 ...)
3549{
3550 STDDCL;
3551 struct ppp_header *h;
3552 struct lcp_header *lh;
3553 struct mbuf *m;
3554 u_char *p;
3555 int len;
3556 unsigned mlen;
3557 const char *msg;
3558 va_list ap;
3559
3560 MGETHDR (m, M_DONTWAIT, MT_DATA);
3561 if (! m)
3562 return;
3563 m->m_pkthdr.rcvif = 0;
3564
3565 h = mtod (m, struct ppp_header*);
3566 h->address = PPP_ALLSTATIONS; /* broadcast address */
3567 h->control = PPP_UI; /* Unnumbered Info */
3568 h->protocol = htons(cp->proto);
3569
3570 lh = (struct lcp_header*)(h + 1);
3571 lh->type = type;
3572 lh->ident = id;
3573 p = (u_char*) (lh+1);
3574
3575 va_start(ap, id);
3576 len = 0;
3577
3578 while ((mlen = va_arg(ap, unsigned)) != 0) {
3579 msg = va_arg(ap, const char *);
3580 len += mlen;
3581 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3582 va_end(ap);
3583 m_freem(m);
3584 return;
3585 }
3586
3587 bcopy(msg, p, mlen);
3588 p += mlen;
3589 }
3590 va_end(ap);
3591
3592 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3593 lh->len = htons (LCP_HEADER_LEN + len);
3594
3595 if (debug) {
3596 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
3597 ifp->if_name, ifp->if_unit, cp->name,
3598 sppp_auth_type_name(cp->proto, lh->type),
3599 lh->ident, ntohs(lh->len));
3600 if (len)
3601 sppp_print_bytes((u_char*) (lh+1), len);
3602 addlog(">\n");
3603 }
3604 if (IF_QFULL (&sp->pp_cpq)) {
3605 IF_DROP (&sp->pp_fastq);
3606 IF_DROP (&ifp->if_snd);
3607 m_freem (m);
3608 ++ifp->if_oerrors;
3609 } else
3610 IF_ENQUEUE (&sp->pp_cpq, m);
3611 if (! (ifp->if_flags & IFF_OACTIVE))
3612 (*ifp->if_start) (ifp);
3613 ifp->if_obytes += m->m_pkthdr.len + 3;
3614}
3615
3616/*
3617 * Flush interface queue.
3618 */
3619static void
3620sppp_qflush(struct ifqueue *ifq)
3621{
3622 struct mbuf *m, *n;
3623
3624 n = ifq->ifq_head;
3625 while ((m = n)) {
3626 n = m->m_act;
3627 m_freem (m);
3628 }
3629 ifq->ifq_head = 0;
3630 ifq->ifq_tail = 0;
3631 ifq->ifq_len = 0;
3632}
3633
3634/*
3635 * Send keepalive packets, every 10 seconds.
3636 */
3637static void
3638sppp_keepalive(void *dummy)
3639{
3640 struct sppp *sp;
3641 int s;
3642
3643 s = splimp();
3644 for (sp=spppq; sp; sp=sp->pp_next) {
3645 struct ifnet *ifp = &sp->pp_if;
3646
3647 /* Keepalive mode disabled or channel down? */
3648 if (! (sp->pp_flags & PP_KEEPALIVE) ||
3649 ! (ifp->if_flags & IFF_RUNNING))
3650 continue;
3651
3652 /* No keepalive in PPP mode if LCP not opened yet. */
3653 if (! (sp->pp_flags & PP_CISCO) &&
3654 sp->pp_phase < PHASE_AUTHENTICATE)
3655 continue;
3656
3657 if (sp->pp_alivecnt == MAXALIVECNT) {
3658 /* No keepalive packets got. Stop the interface. */
3659 printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
3660 if_down (ifp);
3661 sppp_qflush (&sp->pp_cpq);
3662 if (! (sp->pp_flags & PP_CISCO)) {
3663 /* XXX */
3664 /* Shut down the PPP link. */
3665 lcp.Down(sp);
3666 /* Initiate negotiation. XXX */
3667 lcp.Up(sp);
3668 }
3669 }
3670 if (sp->pp_alivecnt <= MAXALIVECNT)
3671 ++sp->pp_alivecnt;
3672 if (sp->pp_flags & PP_CISCO)
3673 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3674 sp->pp_rseq);
3675 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3676 long nmagic = htonl (sp->lcp.magic);
3677 sp->lcp.echoid = ++sp->pp_seq;
3678 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3679 sp->lcp.echoid, 4, &nmagic);
3680 }
3681 }
3682 splx(s);
3683 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
3684}
3685
3686/*
3687 * Get both IP addresses.
3688 */
3689static void
3690sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
3691{
3692 struct ifnet *ifp = &sp->pp_if;
3693 struct ifaddr *ifa;
3694 struct sockaddr_in *si, *sm;
3695 u_long ssrc, ddst;
3696
3697 ssrc = ddst = 0L;
3698 /*
3699 * Pick the first AF_INET address from the list,
3700 * aliases don't make any sense on a p2p link anyway.
3701 */
3702 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3703 ifa;
3704 ifa = ifa->ifa_link.tqe_next)
3705 if (ifa->ifa_addr->sa_family == AF_INET) {
3706 si = (struct sockaddr_in *)ifa->ifa_addr;
3707 sm = (struct sockaddr_in *)ifa->ifa_netmask;
3708 if (si)
3709 break;
3710 }
3711 if (ifa) {
3712 if (si && si->sin_addr.s_addr) {
3713 ssrc = si->sin_addr.s_addr;
3714 if (srcmask)
3715 *srcmask = ntohl(sm->sin_addr.s_addr);
3716 }
3717
3718 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3719 if (si && si->sin_addr.s_addr)
3720 ddst = si->sin_addr.s_addr;
3721 }
3722
3723 if (dst) *dst = ntohl(ddst);
3724 if (src) *src = ntohl(ssrc);
3725}
3726
3727/*
3728 * Set my IP address. Must be called at splimp.
3729 */
3730static void
3731sppp_set_ip_addr(struct sppp *sp, u_long src)
3732{
3733 struct ifnet *ifp = &sp->pp_if;
3734 struct ifaddr *ifa;
3735 struct sockaddr_in *si;
3736 u_long ssrc, ddst;
3737
3738 /*
3739 * Pick the first AF_INET address from the list,
3740 * aliases don't make any sense on a p2p link anyway.
3741 */
3742 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3743 ifa;
3744 ifa = ifa->ifa_link.tqe_next)
3745 if (ifa->ifa_addr->sa_family == AF_INET) {
3746 si = (struct sockaddr_in *)ifa->ifa_addr;
3747 if (si)
3748 break;
3749 }
3750 if (ifa && si)
3751 si->sin_addr.s_addr = htonl(src);
3752}
3753
3754static int
3755sppp_params(struct sppp *sp, int cmd, void *data)
3756{
3757 int subcmd;
3758 struct ifreq *ifr = (struct ifreq *)data;
3759 struct spppreq spr;
3760
3761 /*
3762 * ifr->ifr_data is supposed to point to a struct spppreq.
3763 * Check the cmd word first before attempting to fetch all the
3764 * data.
3765 */
3766 if ((subcmd = fuword(ifr->ifr_data)) == -1)
3767 return EFAULT;
3768
3769 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3770 return EFAULT;
3771
3772 switch (subcmd) {
3773 case SPPPIOGDEFS:
3774 if (cmd != SIOCGIFGENERIC)
3775 return EINVAL;
3776 /*
3777 * We copy over the entire current state, but clean
3778 * out some of the stuff we don't wanna pass up.
3779 * Remember, SIOCGIFGENERIC is unprotected, and can be
3780 * called by any user. No need to ever get PAP or
3781 * CHAP secrets back to userland anyway.
3782 */
3783 bcopy(sp, &spr.defs, sizeof(struct sppp));
3784 bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3785 bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3786 bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3787 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3788 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3789
3790 case SPPPIOSDEFS:
3791 if (cmd != SIOCSIFGENERIC)
3792 return EINVAL;
3793 /*
3794 * We have a very specific idea of which fields we allow
3795 * being passed back from userland, so to not clobber our
3796 * current state. For one, we only allow setting
3797 * anything if LCP is in dead phase. Once the LCP
3798 * negotiations started, the authentication settings must
3799 * not be changed again. (The administrator can force an
3800 * ifconfig down in order to get LCP back into dead
3801 * phase.)
3802 *
3803 * Also, we only allow for authentication parameters to be
3804 * specified.
3805 *
3806 * XXX Should allow to set or clear pp_flags.
3807 *
3808 * Finally, if the respective authentication protocol to
3809 * be used is set differently than 0, but the secret is
3810 * passed as all zeros, we don't trash the existing secret.
3811 * This allows an administrator to change the system name
3812 * only without clobbering the secret (which he didn't get
3813 * back in a previous SPPPIOGDEFS call). However, the
3814 * secrets are cleared if the authentication protocol is
3815 * reset to 0.
3816 */
3817 if (sp->pp_phase != PHASE_DEAD)
3818 return EBUSY;
3819
3820 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
3821 spr.defs.myauth.proto != PPP_CHAP) ||
3822 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
3823 spr.defs.hisauth.proto != PPP_CHAP))
3824 return EINVAL;
3825
3826 if (spr.defs.myauth.proto == 0)
3827 /* resetting myauth */
3828 bzero(&sp->myauth, sizeof sp->myauth);
3829 else {
3830 /* setting/changing myauth */
3831 sp->myauth.proto = spr.defs.myauth.proto;
3832 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
3833 if (spr.defs.myauth.secret[0] != '\0')
3834 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
3835 AUTHKEYLEN);
3836 }
3837 if (spr.defs.hisauth.proto == 0)
3838 /* resetting hisauth */
3839 bzero(&sp->hisauth, sizeof sp->hisauth);
3840 else {
3841 /* setting/changing hisauth */
3842 sp->hisauth.proto = spr.defs.hisauth.proto;
3843 sp->hisauth.flags = spr.defs.hisauth.flags;
3844 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
3845 if (spr.defs.hisauth.secret[0] != '\0')
3846 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
3847 AUTHKEYLEN);
3848 }
3849 break;
3850
3851 default:
3852 return EINVAL;
3853 }
3854
3855 return 0;
3856}
3857
3858static void
3859sppp_phase_network(struct sppp *sp)
3860{
3861 struct ifnet *ifp = &sp->pp_if;
3862 int i;
3863 u_long mask;
3864
3865 sp->pp_phase = PHASE_NETWORK;
3866
3867 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
3868 sppp_phase_name(sp->pp_phase));
3869
3870 /* Notify NCPs now. */
3871 for (i = 0; i < IDX_COUNT; i++)
3872 if ((cps[i])->flags & CP_NCP)
3873 (cps[i])->Open(sp);
3874
3875 /* Send Up events to all NCPs. */
3876 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
3877 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
3878 (cps[i])->Up(sp);
3879
3880 /* if no NCP is starting, all this was in vain, close down */
3881 sppp_lcp_check_and_close(sp);
3882}
3883
3884
3885static const char *
3886sppp_cp_type_name(u_char type)
3887{
3888 static char buf[12];
3889 switch (type) {
3890 case CONF_REQ: return "conf-req";
3891 case CONF_ACK: return "conf-ack";
3892 case CONF_NAK: return "conf-nak";
3893 case CONF_REJ: return "conf-rej";
3894 case TERM_REQ: return "term-req";
3895 case TERM_ACK: return "term-ack";
3896 case CODE_REJ: return "code-rej";
3897 case PROTO_REJ: return "proto-rej";
3898 case ECHO_REQ: return "echo-req";
3899 case ECHO_REPLY: return "echo-reply";
3900 case DISC_REQ: return "discard-req";
3901 }
3902 sprintf (buf, "0x%x", type);
3903 return buf;
3904}
3905
3906static const char *
3907sppp_auth_type_name(u_short proto, u_char type)
3908{
3909 static char buf[12];
3910 switch (proto) {
3911 case PPP_CHAP:
3912 switch (type) {
3913 case CHAP_CHALLENGE: return "challenge";
3914 case CHAP_RESPONSE: return "response";
3915 case CHAP_SUCCESS: return "success";
3916 case CHAP_FAILURE: return "failure";
3917 }
3918 case PPP_PAP:
3919 switch (type) {
3920 case PAP_REQ: return "req";
3921 case PAP_ACK: return "ack";
3922 case PAP_NAK: return "nak";
3923 }
3924 }
3925 sprintf (buf, "0x%x", type);
3926 return buf;
3927}
3928
3929static const char *
3930sppp_lcp_opt_name(u_char opt)
3931{
3932 static char buf[12];
3933 switch (opt) {
3934 case LCP_OPT_MRU: return "mru";
3935 case LCP_OPT_ASYNC_MAP: return "async-map";
3936 case LCP_OPT_AUTH_PROTO: return "auth-proto";
3937 case LCP_OPT_QUAL_PROTO: return "qual-proto";
3938 case LCP_OPT_MAGIC: return "magic";
3939 case LCP_OPT_PROTO_COMP: return "proto-comp";
3940 case LCP_OPT_ADDR_COMP: return "addr-comp";
3941 }
3942 sprintf (buf, "0x%x", opt);
3943 return buf;
3944}
3945
3946static const char *
3947sppp_ipcp_opt_name(u_char opt)
3948{
3949 static char buf[12];
3950 switch (opt) {
3951 case IPCP_OPT_ADDRESSES: return "addresses";
3952 case IPCP_OPT_COMPRESSION: return "compression";
3953 case IPCP_OPT_ADDRESS: return "address";
3954 }
3955 sprintf (buf, "0x%x", opt);
3956 return buf;
3957}
3958
3959static const char *
3960sppp_state_name(int state)
3961{
3962 switch (state) {
3963 case STATE_INITIAL: return "initial";
3964 case STATE_STARTING: return "starting";
3965 case STATE_CLOSED: return "closed";
3966 case STATE_STOPPED: return "stopped";
3967 case STATE_CLOSING: return "closing";
3968 case STATE_STOPPING: return "stopping";
3969 case STATE_REQ_SENT: return "req-sent";
3970 case STATE_ACK_RCVD: return "ack-rcvd";
3971 case STATE_ACK_SENT: return "ack-sent";
3972 case STATE_OPENED: return "opened";
3973 }
3974 return "illegal";
3975}
3976
3977static const char *
3978sppp_phase_name(enum ppp_phase phase)
3979{
3980 switch (phase) {
3981 case PHASE_DEAD: return "dead";
3982 case PHASE_ESTABLISH: return "establish";
3983 case PHASE_TERMINATE: return "terminate";
3984 case PHASE_AUTHENTICATE: return "authenticate";
3985 case PHASE_NETWORK: return "network";
3986 }
3987 return "illegal";
3988}
3989
3990static const char *
3991sppp_proto_name(u_short proto)
3992{
3993 static char buf[12];
3994 switch (proto) {
3995 case PPP_LCP: return "lcp";
3996 case PPP_IPCP: return "ipcp";
3997 case PPP_PAP: return "pap";
3998 case PPP_CHAP: return "chap";
3999 }
4000 sprintf(buf, "0x%x", (unsigned)proto);
4001 return buf;
4002}
4003
4004static void
4005sppp_print_bytes(const u_char *p, u_short len)
4006{
4007 addlog(" %x", *p++);
4008 while (--len > 0)
4009 addlog("-%x", *p++);
4010}
4011
4012static void
4013sppp_print_string(const char *p, u_short len)
4014{
4015 u_char c;
4016
4017 while (len-- > 0) {
4018 c = *p++;
4019 /*
4020 * Print only ASCII chars directly. RFC 1994 recommends
4021 * using only them, but we don't rely on it. */
4022 if (c < ' ' || c > '~')
4023 addlog("\\x%x", c);
4024 else
4025 addlog("%c", c);
4026 }
4027}
4028
4029static const char *
4030sppp_dotted_quad(u_long addr)
4031{
4032 static char s[16];
4033 sprintf(s, "%d.%d.%d.%d",
4034 (addr >> 24) & 0xff,
4035 (addr >> 16) & 0xff,
4036 (addr >> 8) & 0xff,
4037 addr & 0xff);
4038 return s;
4039}
4040
4041static int
4042sppp_strnlen(u_char *p, int max)
4043{
4044 int len;
4045
4046 for (len = 0; len < max && *p; ++p)
4047 ++len;
4048 return len;
4049}
4050
4051/* a dummy, used to drop uninteresting events */
4052static void
4053sppp_null(struct sppp *unused)
4054{
4055 /* do just nothing */
4056}
4057/*
4058 * This file is large. Tell emacs to highlight it nevertheless.
4059 *
4060 * Local Variables:
4061 * hilit-auto-highlight-maxout: 120000
4062 * End:
4063 */
2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
4 *
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 *
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, Joerg Wunsch.
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
19 *
20 * $Id: if_spppsubr.c,v 1.29 1997/12/15 20:31:08 eivind Exp $
21 */
22
23#include "opt_ipx.h"
24
25#include <sys/param.h>
26#include <sys/systm.h>
27#include <sys/kernel.h>
28#include <sys/sockio.h>
29#include <sys/socket.h>
30#include <sys/syslog.h>
31#include <sys/malloc.h>
32#include <sys/mbuf.h>
33#include <sys/md5.h>
34
35#include <net/if.h>
36#include <net/netisr.h>
37#include <net/if_types.h>
38
39#include <machine/stdarg.h>
40
41#ifdef INET
42#include <netinet/in.h>
43#include <netinet/in_systm.h>
44#include <netinet/in_var.h>
45#include <netinet/ip.h>
46#include <netinet/tcp.h>
47#include <netinet/if_ether.h>
48#endif
49
50#ifdef IPX
51#include <netipx/ipx.h>
52#include <netipx/ipx_if.h>
53#endif
54
55#ifdef NS
56#include <netns/ns.h>
57#include <netns/ns_if.h>
58#endif
59
60#ifdef ISO
61#include <netiso/argo_debug.h>
62#include <netiso/iso.h>
63#include <netiso/iso_var.h>
64#include <netiso/iso_snpac.h>
65#endif
66
67#include <net/if_sppp.h>
68
69#define MAXALIVECNT 3 /* max. alive packets */
70
71/*
72 * Interface flags that can be set in an ifconfig command.
73 *
74 * Setting link0 will make the link passive, i.e. it will be marked
75 * as being administrative openable, but won't be opened to begin
76 * with. Incoming calls will be answered, or subsequent calls with
77 * -link1 will cause the administrative open of the LCP layer.
78 *
79 * Setting link1 will cause the link to auto-dial only as packets
80 * arrive to be sent.
81 *
82 * Setting IFF_DEBUG will syslog the option negotiation and state
83 * transitions at level kern.debug. Note: all logs consistently look
84 * like
85 *
86 * <if-name><unit>: <proto-name> <additional info...>
87 *
88 * with <if-name><unit> being something like "bppp0", and <proto-name>
89 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
90 */
91
92#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
93#define IFF_AUTO IFF_LINK1 /* auto-dial on output */
94
95#define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
96#define PPP_UI 0x03 /* Unnumbered Information */
97#define PPP_IP 0x0021 /* Internet Protocol */
98#define PPP_ISO 0x0023 /* ISO OSI Protocol */
99#define PPP_XNS 0x0025 /* Xerox NS Protocol */
100#define PPP_IPX 0x002b /* Novell IPX Protocol */
101#define PPP_LCP 0xc021 /* Link Control Protocol */
102#define PPP_PAP 0xc023 /* Password Authentication Protocol */
103#define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
104#define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
105
106#define CONF_REQ 1 /* PPP configure request */
107#define CONF_ACK 2 /* PPP configure acknowledge */
108#define CONF_NAK 3 /* PPP configure negative ack */
109#define CONF_REJ 4 /* PPP configure reject */
110#define TERM_REQ 5 /* PPP terminate request */
111#define TERM_ACK 6 /* PPP terminate acknowledge */
112#define CODE_REJ 7 /* PPP code reject */
113#define PROTO_REJ 8 /* PPP protocol reject */
114#define ECHO_REQ 9 /* PPP echo request */
115#define ECHO_REPLY 10 /* PPP echo reply */
116#define DISC_REQ 11 /* PPP discard request */
117
118#define LCP_OPT_MRU 1 /* maximum receive unit */
119#define LCP_OPT_ASYNC_MAP 2 /* async control character map */
120#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
121#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
122#define LCP_OPT_MAGIC 5 /* magic number */
123#define LCP_OPT_RESERVED 6 /* reserved */
124#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
125#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
126
127#define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
128#define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
129#define IPCP_OPT_ADDRESS 3 /* local IP address */
130
131#define PAP_REQ 1 /* PAP name/password request */
132#define PAP_ACK 2 /* PAP acknowledge */
133#define PAP_NAK 3 /* PAP fail */
134
135#define CHAP_CHALLENGE 1 /* CHAP challenge request */
136#define CHAP_RESPONSE 2 /* CHAP challenge response */
137#define CHAP_SUCCESS 3 /* CHAP response ok */
138#define CHAP_FAILURE 4 /* CHAP response failed */
139
140#define CHAP_MD5 5 /* hash algorithm - MD5 */
141
142#define CISCO_MULTICAST 0x8f /* Cisco multicast address */
143#define CISCO_UNICAST 0x0f /* Cisco unicast address */
144#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
145#define CISCO_ADDR_REQ 0 /* Cisco address request */
146#define CISCO_ADDR_REPLY 1 /* Cisco address reply */
147#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
148
149/* states are named and numbered according to RFC 1661 */
150#define STATE_INITIAL 0
151#define STATE_STARTING 1
152#define STATE_CLOSED 2
153#define STATE_STOPPED 3
154#define STATE_CLOSING 4
155#define STATE_STOPPING 5
156#define STATE_REQ_SENT 6
157#define STATE_ACK_RCVD 7
158#define STATE_ACK_SENT 8
159#define STATE_OPENED 9
160
161struct ppp_header {
162 u_char address;
163 u_char control;
164 u_short protocol;
165};
166#define PPP_HEADER_LEN sizeof (struct ppp_header)
167
168struct lcp_header {
169 u_char type;
170 u_char ident;
171 u_short len;
172};
173#define LCP_HEADER_LEN sizeof (struct lcp_header)
174
175struct cisco_packet {
176 u_long type;
177 u_long par1;
178 u_long par2;
179 u_short rel;
180 u_short time0;
181 u_short time1;
182};
183#define CISCO_PACKET_LEN 18
184
185/*
186 * We follow the spelling and capitalization of RFC 1661 here, to make
187 * it easier comparing with the standard. Please refer to this RFC in
188 * case you can't make sense out of these abbreviation; it will also
189 * explain the semantics related to the various events and actions.
190 */
191struct cp {
192 u_short proto; /* PPP control protocol number */
193 u_char protoidx; /* index into state table in struct sppp */
194 u_char flags;
195#define CP_LCP 0x01 /* this is the LCP */
196#define CP_AUTH 0x02 /* this is an authentication protocol */
197#define CP_NCP 0x04 /* this is a NCP */
198#define CP_QUAL 0x08 /* this is a quality reporting protocol */
199 const char *name; /* name of this control protocol */
200 /* event handlers */
201 void (*Up)(struct sppp *sp);
202 void (*Down)(struct sppp *sp);
203 void (*Open)(struct sppp *sp);
204 void (*Close)(struct sppp *sp);
205 void (*TO)(void *sp);
206 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
207 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
208 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
209 /* actions */
210 void (*tlu)(struct sppp *sp);
211 void (*tld)(struct sppp *sp);
212 void (*tls)(struct sppp *sp);
213 void (*tlf)(struct sppp *sp);
214 void (*scr)(struct sppp *sp);
215};
216
217static struct sppp *spppq;
218static struct callout_handle keepalive_ch;
219
220/*
221 * The following disgusting hack gets around the problem that IP TOS
222 * can't be set yet. We want to put "interactive" traffic on a high
223 * priority queue. To decide if traffic is interactive, we check that
224 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
225 *
226 * XXX is this really still necessary? - joerg -
227 */
228static u_short interactive_ports[8] = {
229 0, 513, 0, 0,
230 0, 21, 0, 23,
231};
232#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
233
234/* almost every function needs these */
235#define STDDCL \
236 struct ifnet *ifp = &sp->pp_if; \
237 int debug = ifp->if_flags & IFF_DEBUG
238
239static int sppp_output(struct ifnet *ifp, struct mbuf *m,
240 struct sockaddr *dst, struct rtentry *rt);
241
242static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
243static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
244
245static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
246 struct mbuf *m);
247static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
248 u_char ident, u_short len, void *data);
249static void sppp_cp_timeout(void *arg);
250static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
251 int newstate);
252static void sppp_auth_send(const struct cp *cp,
253 struct sppp *sp, u_char type, u_char id,
254 ...);
255
256static void sppp_up_event(const struct cp *cp, struct sppp *sp);
257static void sppp_down_event(const struct cp *cp, struct sppp *sp);
258static void sppp_open_event(const struct cp *cp, struct sppp *sp);
259static void sppp_close_event(const struct cp *cp, struct sppp *sp);
260static void sppp_to_event(const struct cp *cp, struct sppp *sp);
261
262static void sppp_null(struct sppp *sp);
263
264static void sppp_lcp_init(struct sppp *sp);
265static void sppp_lcp_up(struct sppp *sp);
266static void sppp_lcp_down(struct sppp *sp);
267static void sppp_lcp_open(struct sppp *sp);
268static void sppp_lcp_close(struct sppp *sp);
269static void sppp_lcp_TO(void *sp);
270static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
271static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
272static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
273static void sppp_lcp_tlu(struct sppp *sp);
274static void sppp_lcp_tld(struct sppp *sp);
275static void sppp_lcp_tls(struct sppp *sp);
276static void sppp_lcp_tlf(struct sppp *sp);
277static void sppp_lcp_scr(struct sppp *sp);
278static void sppp_lcp_check_and_close(struct sppp *sp);
279static int sppp_ncp_check(struct sppp *sp);
280
281static void sppp_ipcp_init(struct sppp *sp);
282static void sppp_ipcp_up(struct sppp *sp);
283static void sppp_ipcp_down(struct sppp *sp);
284static void sppp_ipcp_open(struct sppp *sp);
285static void sppp_ipcp_close(struct sppp *sp);
286static void sppp_ipcp_TO(void *sp);
287static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
288static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
289static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
290static void sppp_ipcp_tlu(struct sppp *sp);
291static void sppp_ipcp_tld(struct sppp *sp);
292static void sppp_ipcp_tls(struct sppp *sp);
293static void sppp_ipcp_tlf(struct sppp *sp);
294static void sppp_ipcp_scr(struct sppp *sp);
295
296static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
297static void sppp_pap_init(struct sppp *sp);
298static void sppp_pap_open(struct sppp *sp);
299static void sppp_pap_close(struct sppp *sp);
300static void sppp_pap_TO(void *sp);
301static void sppp_pap_my_TO(void *sp);
302static void sppp_pap_tlu(struct sppp *sp);
303static void sppp_pap_tld(struct sppp *sp);
304static void sppp_pap_scr(struct sppp *sp);
305
306static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
307static void sppp_chap_init(struct sppp *sp);
308static void sppp_chap_open(struct sppp *sp);
309static void sppp_chap_close(struct sppp *sp);
310static void sppp_chap_TO(void *sp);
311static void sppp_chap_tlu(struct sppp *sp);
312static void sppp_chap_tld(struct sppp *sp);
313static void sppp_chap_scr(struct sppp *sp);
314
315static const char *sppp_auth_type_name(u_short proto, u_char type);
316static const char *sppp_cp_type_name(u_char type);
317static const char *sppp_dotted_quad(u_long addr);
318static const char *sppp_ipcp_opt_name(u_char opt);
319static const char *sppp_lcp_opt_name(u_char opt);
320static const char *sppp_phase_name(enum ppp_phase phase);
321static const char *sppp_proto_name(u_short proto);
322static const char *sppp_state_name(int state);
323static int sppp_params(struct sppp *sp, int cmd, void *data);
324static int sppp_strnlen(u_char *p, int max);
325static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
326 u_long *srcmask);
327static void sppp_keepalive(void *dummy);
328static void sppp_phase_network(struct sppp *sp);
329static void sppp_print_bytes(const u_char *p, u_short len);
330static void sppp_print_string(const char *p, u_short len);
331static void sppp_qflush(struct ifqueue *ifq);
332static void sppp_set_ip_addr(struct sppp *sp, u_long src);
333
334/* our control protocol descriptors */
335const struct cp lcp = {
336 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
337 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
338 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
339 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
340 sppp_lcp_scr
341};
342
343const struct cp ipcp = {
344 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
345 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
346 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
347 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
348 sppp_ipcp_scr
349};
350
351const struct cp pap = {
352 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
353 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
354 sppp_pap_TO, 0, 0, 0,
355 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
356 sppp_pap_scr
357};
358
359const struct cp chap = {
360 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
361 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
362 sppp_chap_TO, 0, 0, 0,
363 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
364 sppp_chap_scr
365};
366
367const struct cp *cps[IDX_COUNT] = {
368 &lcp, /* IDX_LCP */
369 &ipcp, /* IDX_IPCP */
370 &pap, /* IDX_PAP */
371 &chap, /* IDX_CHAP */
372};
373
374
375/*
376 * Exported functions, comprising our interface to the lower layer.
377 */
378
379/*
380 * Process the received packet.
381 */
382void
383sppp_input(struct ifnet *ifp, struct mbuf *m)
384{
385 struct ppp_header *h;
386 struct ifqueue *inq = 0;
387 int s;
388 struct sppp *sp = (struct sppp *)ifp;
389 int debug = ifp->if_flags & IFF_DEBUG;
390
391 if (ifp->if_flags & IFF_UP)
392 /* Count received bytes, add FCS and one flag */
393 ifp->if_ibytes += m->m_pkthdr.len + 3;
394
395 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
396 /* Too small packet, drop it. */
397 if (debug)
398 log(LOG_DEBUG,
399 "%s%d: input packet is too small, %d bytes\n",
400 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
401 drop:
402 ++ifp->if_ierrors;
403 ++ifp->if_iqdrops;
404 m_freem (m);
405 return;
406 }
407
408 /* Get PPP header. */
409 h = mtod (m, struct ppp_header*);
410 m_adj (m, PPP_HEADER_LEN);
411
412 switch (h->address) {
413 case PPP_ALLSTATIONS:
414 if (h->control != PPP_UI)
415 goto invalid;
416 if (sp->pp_flags & PP_CISCO) {
417 if (debug)
418 log(LOG_DEBUG,
419 "%s%d: PPP packet in Cisco mode "
420 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
421 ifp->if_name, ifp->if_unit,
422 h->address, h->control, ntohs(h->protocol));
423 goto drop;
424 }
425 switch (ntohs (h->protocol)) {
426 default:
427 if (sp->state[IDX_LCP] == STATE_OPENED)
428 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
429 ++sp->pp_seq, m->m_pkthdr.len + 2,
430 &h->protocol);
431 if (debug)
432 log(LOG_DEBUG,
433 "%s%d: invalid input protocol "
434 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
435 ifp->if_name, ifp->if_unit,
436 h->address, h->control, ntohs(h->protocol));
437 ++ifp->if_noproto;
438 goto drop;
439 case PPP_LCP:
440 sppp_cp_input(&lcp, sp, m);
441 m_freem (m);
442 return;
443 case PPP_PAP:
444 if (sp->pp_phase >= PHASE_AUTHENTICATE)
445 sppp_pap_input(sp, m);
446 m_freem (m);
447 return;
448 case PPP_CHAP:
449 if (sp->pp_phase >= PHASE_AUTHENTICATE)
450 sppp_chap_input(sp, m);
451 m_freem (m);
452 return;
453#ifdef INET
454 case PPP_IPCP:
455 if (sp->pp_phase == PHASE_NETWORK)
456 sppp_cp_input(&ipcp, sp, m);
457 m_freem (m);
458 return;
459 case PPP_IP:
460 if (sp->state[IDX_IPCP] == STATE_OPENED) {
461 schednetisr (NETISR_IP);
462 inq = &ipintrq;
463 }
464 break;
465#endif
466#ifdef IPX
467 case PPP_IPX:
468 /* IPX IPXCP not implemented yet */
469 if (sp->pp_phase == PHASE_NETWORK) {
470 schednetisr (NETISR_IPX);
471 inq = &ipxintrq;
472 }
473 break;
474#endif
475#ifdef NS
476 case PPP_XNS:
477 /* XNS IDPCP not implemented yet */
478 if (sp->pp_phase == PHASE_NETWORK) {
479 schednetisr (NETISR_NS);
480 inq = &nsintrq;
481 }
482 break;
483#endif
484#ifdef ISO
485 case PPP_ISO:
486 /* OSI NLCP not implemented yet */
487 if (sp->pp_phase == PHASE_NETWORK) {
488 schednetisr (NETISR_ISO);
489 inq = &clnlintrq;
490 }
491 break;
492#endif
493 }
494 break;
495 case CISCO_MULTICAST:
496 case CISCO_UNICAST:
497 /* Don't check the control field here (RFC 1547). */
498 if (! (sp->pp_flags & PP_CISCO)) {
499 if (debug)
500 log(LOG_DEBUG,
501 "%s%d: Cisco packet in PPP mode "
502 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
503 ifp->if_name, ifp->if_unit,
504 h->address, h->control, ntohs(h->protocol));
505 goto drop;
506 }
507 switch (ntohs (h->protocol)) {
508 default:
509 ++ifp->if_noproto;
510 goto invalid;
511 case CISCO_KEEPALIVE:
512 sppp_cisco_input ((struct sppp*) ifp, m);
513 m_freem (m);
514 return;
515#ifdef INET
516 case ETHERTYPE_IP:
517 schednetisr (NETISR_IP);
518 inq = &ipintrq;
519 break;
520#endif
521#ifdef IPX
522 case ETHERTYPE_IPX:
523 schednetisr (NETISR_IPX);
524 inq = &ipxintrq;
525 break;
526#endif
527#ifdef NS
528 case ETHERTYPE_NS:
529 schednetisr (NETISR_NS);
530 inq = &nsintrq;
531 break;
532#endif
533 }
534 break;
535 default: /* Invalid PPP packet. */
536 invalid:
537 if (debug)
538 log(LOG_DEBUG,
539 "%s%d: invalid input packet "
540 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
541 ifp->if_name, ifp->if_unit,
542 h->address, h->control, ntohs(h->protocol));
543 goto drop;
544 }
545
546 if (! (ifp->if_flags & IFF_UP) || ! inq)
547 goto drop;
548
549 /* Check queue. */
550 s = splimp();
551 if (IF_QFULL (inq)) {
552 /* Queue overflow. */
553 IF_DROP(inq);
554 splx(s);
555 if (debug)
556 log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
557 ifp->if_name, ifp->if_unit);
558 goto drop;
559 }
560 IF_ENQUEUE(inq, m);
561 splx(s);
562}
563
564/*
565 * Enqueue transmit packet.
566 */
567static int
568sppp_output(struct ifnet *ifp, struct mbuf *m,
569 struct sockaddr *dst, struct rtentry *rt)
570{
571 struct sppp *sp = (struct sppp*) ifp;
572 struct ppp_header *h;
573 struct ifqueue *ifq;
574 int s, rv = 0;
575
576 s = splimp();
577
578 if ((ifp->if_flags & IFF_UP) == 0 ||
579 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
580 m_freem (m);
581 splx (s);
582 return (ENETDOWN);
583 }
584
585 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
586 /*
587 * Interface is not yet running, but auto-dial. Need
588 * to start LCP for it.
589 */
590 ifp->if_flags |= IFF_RUNNING;
591 splx(s);
592 lcp.Open(sp);
593 s = splimp();
594 }
595
596 ifq = &ifp->if_snd;
597#ifdef INET
598 /*
599 * Put low delay, telnet, rlogin and ftp control packets
600 * in front of the queue.
601 */
602 if (dst->sa_family == AF_INET) {
603 struct ip *ip = mtod (m, struct ip*);
604 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
605
606 if (! IF_QFULL (&sp->pp_fastq) &&
607 ((ip->ip_tos & IPTOS_LOWDELAY) ||
608 ip->ip_p == IPPROTO_TCP &&
609 m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
610 (INTERACTIVE (ntohs (tcp->th_sport)) ||
611 INTERACTIVE (ntohs (tcp->th_dport)))))
612 ifq = &sp->pp_fastq;
613 }
614#endif
615
616 /*
617 * Prepend general data packet PPP header. For now, IP only.
618 */
619 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
620 if (! m) {
621 if (ifp->if_flags & IFF_DEBUG)
622 log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
623 ifp->if_name, ifp->if_unit);
624 ++ifp->if_oerrors;
625 splx (s);
626 return (ENOBUFS);
627 }
628 h = mtod (m, struct ppp_header*);
629 if (sp->pp_flags & PP_CISCO) {
630 h->address = CISCO_UNICAST; /* unicast address */
631 h->control = 0;
632 } else {
633 h->address = PPP_ALLSTATIONS; /* broadcast address */
634 h->control = PPP_UI; /* Unnumbered Info */
635 }
636
637 switch (dst->sa_family) {
638#ifdef INET
639 case AF_INET: /* Internet Protocol */
640 if (sp->pp_flags & PP_CISCO)
641 h->protocol = htons (ETHERTYPE_IP);
642 else {
643 /*
644 * Don't choke with an ENETDOWN early. It's
645 * possible that we just started dialing out,
646 * so don't drop the packet immediately. If
647 * we notice that we run out of buffer space
648 * below, we will however remember that we are
649 * not ready to carry IP packets, and return
650 * ENETDOWN, as opposed to ENOBUFS.
651 */
652 h->protocol = htons(PPP_IP);
653 if (sp->state[IDX_IPCP] != STATE_OPENED)
654 rv = ENETDOWN;
655 }
656 break;
657#endif
658#ifdef NS
659 case AF_NS: /* Xerox NS Protocol */
660 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
661 ETHERTYPE_NS : PPP_XNS);
662 break;
663#endif
664#ifdef IPX
665 case AF_IPX: /* Novell IPX Protocol */
666 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
667 ETHERTYPE_IPX : PPP_IPX);
668 break;
669#endif
670#ifdef ISO
671 case AF_ISO: /* ISO OSI Protocol */
672 if (sp->pp_flags & PP_CISCO)
673 goto nosupport;
674 h->protocol = htons (PPP_ISO);
675 break;
676nosupport:
677#endif
678 default:
679 m_freem (m);
680 ++ifp->if_oerrors;
681 splx (s);
682 return (EAFNOSUPPORT);
683 }
684
685 /*
686 * Queue message on interface, and start output if interface
687 * not yet active.
688 */
689 if (IF_QFULL (ifq)) {
690 IF_DROP (&ifp->if_snd);
691 m_freem (m);
692 ++ifp->if_oerrors;
693 splx (s);
694 return (rv? rv: ENOBUFS);
695 }
696 IF_ENQUEUE (ifq, m);
697 if (! (ifp->if_flags & IFF_OACTIVE))
698 (*ifp->if_start) (ifp);
699
700 /*
701 * Count output packets and bytes.
702 * The packet length includes header, FCS and 1 flag,
703 * according to RFC 1333.
704 */
705 ifp->if_obytes += m->m_pkthdr.len + 3;
706 splx (s);
707 return (0);
708}
709
710void
711sppp_attach(struct ifnet *ifp)
712{
713 struct sppp *sp = (struct sppp*) ifp;
714
715 /* Initialize keepalive handler. */
716 if (! spppq)
717 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
718
719 /* Insert new entry into the keepalive list. */
720 sp->pp_next = spppq;
721 spppq = sp;
722
723 sp->pp_if.if_type = IFT_PPP;
724 sp->pp_if.if_output = sppp_output;
725 sp->pp_fastq.ifq_maxlen = 32;
726 sp->pp_cpq.ifq_maxlen = 20;
727 sp->pp_loopcnt = 0;
728 sp->pp_alivecnt = 0;
729 sp->pp_seq = 0;
730 sp->pp_rseq = 0;
731 sp->pp_phase = PHASE_DEAD;
732 sp->pp_up = lcp.Up;
733 sp->pp_down = lcp.Down;
734
735 sppp_lcp_init(sp);
736 sppp_ipcp_init(sp);
737 sppp_pap_init(sp);
738 sppp_chap_init(sp);
739}
740
741void
742sppp_detach(struct ifnet *ifp)
743{
744 struct sppp **q, *p, *sp = (struct sppp*) ifp;
745 int i;
746
747 /* Remove the entry from the keepalive list. */
748 for (q = &spppq; (p = *q); q = &p->pp_next)
749 if (p == sp) {
750 *q = p->pp_next;
751 break;
752 }
753
754 /* Stop keepalive handler. */
755 if (! spppq)
756 untimeout(sppp_keepalive, 0, keepalive_ch);
757
758 for (i = 0; i < IDX_COUNT; i++)
759 untimeout((cps[i])->TO, (void *)sp, sp->ch[i]);
760 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
761}
762
763/*
764 * Flush the interface output queue.
765 */
766void
767sppp_flush(struct ifnet *ifp)
768{
769 struct sppp *sp = (struct sppp*) ifp;
770
771 sppp_qflush (&sp->pp_if.if_snd);
772 sppp_qflush (&sp->pp_fastq);
773 sppp_qflush (&sp->pp_cpq);
774}
775
776/*
777 * Check if the output queue is empty.
778 */
779int
780sppp_isempty(struct ifnet *ifp)
781{
782 struct sppp *sp = (struct sppp*) ifp;
783 int empty, s;
784
785 s = splimp();
786 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
787 !sp->pp_if.if_snd.ifq_head;
788 splx(s);
789 return (empty);
790}
791
792/*
793 * Get next packet to send.
794 */
795struct mbuf *
796sppp_dequeue(struct ifnet *ifp)
797{
798 struct sppp *sp = (struct sppp*) ifp;
799 struct mbuf *m;
800 int s;
801
802 s = splimp();
803 /*
804 * Process only the control protocol queue until we have at
805 * least one NCP open.
806 *
807 * Do always serve all three queues in Cisco mode.
808 */
809 IF_DEQUEUE(&sp->pp_cpq, m);
810 if (m == NULL &&
811 (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) {
812 IF_DEQUEUE(&sp->pp_fastq, m);
813 if (m == NULL)
814 IF_DEQUEUE (&sp->pp_if.if_snd, m);
815 }
816 splx(s);
817 return m;
818}
819
820/*
821 * Pick the next packet, do not remove it from the queue.
822 */
823struct mbuf *
824sppp_pick(struct ifnet *ifp)
825{
826 struct sppp *sp = (struct sppp*)ifp;
827 struct mbuf *m;
828 int s;
829
830 s= splimp ();
831
832 m = sp->pp_cpq.ifq_head;
833 if (m == NULL &&
834 (sp->pp_phase == PHASE_NETWORK ||
835 (sp->pp_flags & PP_CISCO) != 0))
836 if ((m = sp->pp_fastq.ifq_head) == NULL)
837 m = sp->pp_if.if_snd.ifq_head;
838 splx (s);
839 return (m);
840}
841
842/*
843 * Process an ioctl request. Called on low priority level.
844 */
845int
846sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
847{
848 struct ifreq *ifr = (struct ifreq*) data;
849 struct sppp *sp = (struct sppp*) ifp;
850 int s, rv, going_up, going_down, newmode;
851
852 s = splimp();
853 rv = 0;
854 switch (cmd) {
855 case SIOCAIFADDR:
856 case SIOCSIFDSTADDR:
857 break;
858
859 case SIOCSIFADDR:
860 if_up(ifp);
861 /* fall through... */
862
863 case SIOCSIFFLAGS:
864 going_up = ifp->if_flags & IFF_UP &&
865 (ifp->if_flags & IFF_RUNNING) == 0;
866 going_down = (ifp->if_flags & IFF_UP) == 0 &&
867 ifp->if_flags & IFF_RUNNING;
868 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
869 if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
870 /* sanity */
871 newmode = IFF_PASSIVE;
872 ifp->if_flags &= ~IFF_AUTO;
873 }
874
875 if (going_up || going_down)
876 lcp.Close(sp);
877 if (going_up && newmode == 0) {
878 /* neither auto-dial nor passive */
879 ifp->if_flags |= IFF_RUNNING;
880 if (!(sp->pp_flags & PP_CISCO))
881 lcp.Open(sp);
882 } else if (going_down) {
883 sppp_flush(ifp);
884 ifp->if_flags &= ~IFF_RUNNING;
885 }
886
887 break;
888
889#ifdef SIOCSIFMTU
890#ifndef ifr_mtu
891#define ifr_mtu ifr_metric
892#endif
893 case SIOCSIFMTU:
894 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
895 return (EINVAL);
896 ifp->if_mtu = ifr->ifr_mtu;
897 break;
898#endif
899#ifdef SLIOCSETMTU
900 case SLIOCSETMTU:
901 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
902 return (EINVAL);
903 ifp->if_mtu = *(short*)data;
904 break;
905#endif
906#ifdef SIOCGIFMTU
907 case SIOCGIFMTU:
908 ifr->ifr_mtu = ifp->if_mtu;
909 break;
910#endif
911#ifdef SLIOCGETMTU
912 case SLIOCGETMTU:
913 *(short*)data = ifp->if_mtu;
914 break;
915#endif
916 case SIOCADDMULTI:
917 case SIOCDELMULTI:
918 break;
919
920 case SIOCGIFGENERIC:
921 case SIOCSIFGENERIC:
922 rv = sppp_params(sp, cmd, data);
923 break;
924
925 default:
926 rv = ENOTTY;
927 }
928 splx(s);
929 return rv;
930}
931
932
933/*
934 * Cisco framing implementation.
935 */
936
937/*
938 * Handle incoming Cisco keepalive protocol packets.
939 */
940static void
941sppp_cisco_input(struct sppp *sp, struct mbuf *m)
942{
943 STDDCL;
944 struct cisco_packet *h;
945 u_long me, mymask;
946
947 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
948 if (debug)
949 log(LOG_DEBUG,
950 "%s%d: cisco invalid packet length: %d bytes\n",
951 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
952 return;
953 }
954 h = mtod (m, struct cisco_packet*);
955 if (debug)
956 log(LOG_DEBUG,
957 "%s%d: cisco input: %d bytes "
958 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
959 ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
960 ntohl (h->type), h->par1, h->par2, h->rel,
961 h->time0, h->time1);
962 switch (ntohl (h->type)) {
963 default:
964 if (debug)
965 addlog("%s%d: cisco unknown packet type: 0x%lx\n",
966 ifp->if_name, ifp->if_unit, ntohl (h->type));
967 break;
968 case CISCO_ADDR_REPLY:
969 /* Reply on address request, ignore */
970 break;
971 case CISCO_KEEPALIVE_REQ:
972 sp->pp_alivecnt = 0;
973 sp->pp_rseq = ntohl (h->par1);
974 if (sp->pp_seq == sp->pp_rseq) {
975 /* Local and remote sequence numbers are equal.
976 * Probably, the line is in loopback mode. */
977 if (sp->pp_loopcnt >= MAXALIVECNT) {
978 printf ("%s%d: loopback\n",
979 ifp->if_name, ifp->if_unit);
980 sp->pp_loopcnt = 0;
981 if (ifp->if_flags & IFF_UP) {
982 if_down (ifp);
983 sppp_qflush (&sp->pp_cpq);
984 }
985 }
986 ++sp->pp_loopcnt;
987
988 /* Generate new local sequence number */
989 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
990 break;
991 }
992 sp->pp_loopcnt = 0;
993 if (! (ifp->if_flags & IFF_UP) &&
994 (ifp->if_flags & IFF_RUNNING)) {
995 if_up(ifp);
996 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
997 }
998 break;
999 case CISCO_ADDR_REQ:
1000 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1001 if (me != 0L)
1002 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1003 break;
1004 }
1005}
1006
1007/*
1008 * Send Cisco keepalive packet.
1009 */
1010static void
1011sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1012{
1013 STDDCL;
1014 struct ppp_header *h;
1015 struct cisco_packet *ch;
1016 struct mbuf *m;
1017 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1018
1019 MGETHDR (m, M_DONTWAIT, MT_DATA);
1020 if (! m)
1021 return;
1022 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1023 m->m_pkthdr.rcvif = 0;
1024
1025 h = mtod (m, struct ppp_header*);
1026 h->address = CISCO_MULTICAST;
1027 h->control = 0;
1028 h->protocol = htons (CISCO_KEEPALIVE);
1029
1030 ch = (struct cisco_packet*) (h + 1);
1031 ch->type = htonl (type);
1032 ch->par1 = htonl (par1);
1033 ch->par2 = htonl (par2);
1034 ch->rel = -1;
1035 ch->time0 = htons ((u_short) (t >> 16));
1036 ch->time1 = htons ((u_short) t);
1037
1038 if (debug)
1039 log(LOG_DEBUG,
1040 "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1041 ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
1042 ch->par2, ch->rel, ch->time0, ch->time1);
1043
1044 if (IF_QFULL (&sp->pp_cpq)) {
1045 IF_DROP (&sp->pp_fastq);
1046 IF_DROP (&ifp->if_snd);
1047 m_freem (m);
1048 } else
1049 IF_ENQUEUE (&sp->pp_cpq, m);
1050 if (! (ifp->if_flags & IFF_OACTIVE))
1051 (*ifp->if_start) (ifp);
1052 ifp->if_obytes += m->m_pkthdr.len + 3;
1053}
1054
1055/*
1056 * PPP protocol implementation.
1057 */
1058
1059/*
1060 * Send PPP control protocol packet.
1061 */
1062static void
1063sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1064 u_char ident, u_short len, void *data)
1065{
1066 STDDCL;
1067 struct ppp_header *h;
1068 struct lcp_header *lh;
1069 struct mbuf *m;
1070
1071 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1072 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1073 MGETHDR (m, M_DONTWAIT, MT_DATA);
1074 if (! m)
1075 return;
1076 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1077 m->m_pkthdr.rcvif = 0;
1078
1079 h = mtod (m, struct ppp_header*);
1080 h->address = PPP_ALLSTATIONS; /* broadcast address */
1081 h->control = PPP_UI; /* Unnumbered Info */
1082 h->protocol = htons (proto); /* Link Control Protocol */
1083
1084 lh = (struct lcp_header*) (h + 1);
1085 lh->type = type;
1086 lh->ident = ident;
1087 lh->len = htons (LCP_HEADER_LEN + len);
1088 if (len)
1089 bcopy (data, lh+1, len);
1090
1091 if (debug) {
1092 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
1093 ifp->if_name, ifp->if_unit,
1094 sppp_proto_name(proto),
1095 sppp_cp_type_name (lh->type), lh->ident,
1096 ntohs (lh->len));
1097 if (len)
1098 sppp_print_bytes ((u_char*) (lh+1), len);
1099 addlog(">\n");
1100 }
1101 if (IF_QFULL (&sp->pp_cpq)) {
1102 IF_DROP (&sp->pp_fastq);
1103 IF_DROP (&ifp->if_snd);
1104 m_freem (m);
1105 ++ifp->if_oerrors;
1106 } else
1107 IF_ENQUEUE (&sp->pp_cpq, m);
1108 if (! (ifp->if_flags & IFF_OACTIVE))
1109 (*ifp->if_start) (ifp);
1110 ifp->if_obytes += m->m_pkthdr.len + 3;
1111}
1112
1113/*
1114 * Handle incoming PPP control protocol packets.
1115 */
1116static void
1117sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1118{
1119 STDDCL;
1120 struct lcp_header *h;
1121 int len = m->m_pkthdr.len;
1122 int rv;
1123 u_char *p;
1124
1125 if (len < 4) {
1126 if (debug)
1127 log(LOG_DEBUG,
1128 "%s%d: %s invalid packet length: %d bytes\n",
1129 ifp->if_name, ifp->if_unit, cp->name, len);
1130 return;
1131 }
1132 h = mtod (m, struct lcp_header*);
1133 if (debug) {
1134 log(LOG_DEBUG,
1135 "%s%d: %s input(%s): <%s id=0x%x len=%d",
1136 ifp->if_name, ifp->if_unit, cp->name,
1137 sppp_state_name(sp->state[cp->protoidx]),
1138 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1139 if (len > 4)
1140 sppp_print_bytes ((u_char*) (h+1), len-4);
1141 addlog(">\n");
1142 }
1143 if (len > ntohs (h->len))
1144 len = ntohs (h->len);
1145 p = (u_char *)(h + 1);
1146 switch (h->type) {
1147 case CONF_REQ:
1148 if (len < 4) {
1149 if (debug)
1150 addlog("%s%d: %s invalid conf-req length %d\n",
1151 ifp->if_name, ifp->if_unit, cp->name,
1152 len);
1153 ++ifp->if_ierrors;
1154 break;
1155 }
1156 /* handle states where RCR doesn't get a SCA/SCN */
1157 switch (sp->state[cp->protoidx]) {
1158 case STATE_CLOSING:
1159 case STATE_STOPPING:
1160 return;
1161 case STATE_CLOSED:
1162 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1163 0, 0);
1164 return;
1165 }
1166 rv = (cp->RCR)(sp, h, len);
1167 switch (sp->state[cp->protoidx]) {
1168 case STATE_OPENED:
1169 (cp->tld)(sp);
1170 (cp->scr)(sp);
1171 /* fall through... */
1172 case STATE_ACK_SENT:
1173 case STATE_REQ_SENT:
1174 sppp_cp_change_state(cp, sp, rv?
1175 STATE_ACK_SENT: STATE_REQ_SENT);
1176 break;
1177 case STATE_STOPPED:
1178 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1179 (cp->scr)(sp);
1180 sppp_cp_change_state(cp, sp, rv?
1181 STATE_ACK_SENT: STATE_REQ_SENT);
1182 break;
1183 case STATE_ACK_RCVD:
1184 if (rv) {
1185 sppp_cp_change_state(cp, sp, STATE_OPENED);
1186 if (debug)
1187 log(LOG_DEBUG, "%s%d: %s tlu\n",
1188 ifp->if_name, ifp->if_unit,
1189 cp->name);
1190 (cp->tlu)(sp);
1191 } else
1192 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1193 break;
1194 default:
1195 printf("%s%d: %s illegal %s in state %s\n",
1196 ifp->if_name, ifp->if_unit, cp->name,
1197 sppp_cp_type_name(h->type),
1198 sppp_state_name(sp->state[cp->protoidx]));
1199 ++ifp->if_ierrors;
1200 }
1201 break;
1202 case CONF_ACK:
1203 if (h->ident != sp->confid[cp->protoidx]) {
1204 if (debug)
1205 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1206 ifp->if_name, ifp->if_unit, cp->name,
1207 h->ident, sp->confid[cp->protoidx]);
1208 ++ifp->if_ierrors;
1209 break;
1210 }
1211 switch (sp->state[cp->protoidx]) {
1212 case STATE_CLOSED:
1213 case STATE_STOPPED:
1214 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1215 break;
1216 case STATE_CLOSING:
1217 case STATE_STOPPING:
1218 break;
1219 case STATE_REQ_SENT:
1220 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1221 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1222 break;
1223 case STATE_OPENED:
1224 (cp->tld)(sp);
1225 /* fall through */
1226 case STATE_ACK_RCVD:
1227 (cp->scr)(sp);
1228 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1229 break;
1230 case STATE_ACK_SENT:
1231 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1232 sppp_cp_change_state(cp, sp, STATE_OPENED);
1233 if (debug)
1234 log(LOG_DEBUG, "%s%d: %s tlu\n",
1235 ifp->if_name, ifp->if_unit, cp->name);
1236 (cp->tlu)(sp);
1237 break;
1238 default:
1239 printf("%s%d: %s illegal %s in state %s\n",
1240 ifp->if_name, ifp->if_unit, cp->name,
1241 sppp_cp_type_name(h->type),
1242 sppp_state_name(sp->state[cp->protoidx]));
1243 ++ifp->if_ierrors;
1244 }
1245 break;
1246 case CONF_NAK:
1247 case CONF_REJ:
1248 if (h->ident != sp->confid[cp->protoidx]) {
1249 if (debug)
1250 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1251 ifp->if_name, ifp->if_unit, cp->name,
1252 h->ident, sp->confid[cp->protoidx]);
1253 ++ifp->if_ierrors;
1254 break;
1255 }
1256 if (h->type == CONF_NAK)
1257 (cp->RCN_nak)(sp, h, len);
1258 else /* CONF_REJ */
1259 (cp->RCN_rej)(sp, h, len);
1260
1261 switch (sp->state[cp->protoidx]) {
1262 case STATE_CLOSED:
1263 case STATE_STOPPED:
1264 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1265 break;
1266 case STATE_REQ_SENT:
1267 case STATE_ACK_SENT:
1268 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1269 (cp->scr)(sp);
1270 break;
1271 case STATE_OPENED:
1272 (cp->tld)(sp);
1273 /* fall through */
1274 case STATE_ACK_RCVD:
1275 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1276 (cp->scr)(sp);
1277 break;
1278 case STATE_CLOSING:
1279 case STATE_STOPPING:
1280 break;
1281 default:
1282 printf("%s%d: %s illegal %s in state %s\n",
1283 ifp->if_name, ifp->if_unit, cp->name,
1284 sppp_cp_type_name(h->type),
1285 sppp_state_name(sp->state[cp->protoidx]));
1286 ++ifp->if_ierrors;
1287 }
1288 break;
1289
1290 case TERM_REQ:
1291 switch (sp->state[cp->protoidx]) {
1292 case STATE_ACK_RCVD:
1293 case STATE_ACK_SENT:
1294 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1295 /* fall through */
1296 case STATE_CLOSED:
1297 case STATE_STOPPED:
1298 case STATE_CLOSING:
1299 case STATE_STOPPING:
1300 case STATE_REQ_SENT:
1301 sta:
1302 /* Send Terminate-Ack packet. */
1303 if (debug)
1304 log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1305 ifp->if_name, ifp->if_unit, cp->name);
1306 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1307 break;
1308 case STATE_OPENED:
1309 (cp->tld)(sp);
1310 sp->rst_counter[cp->protoidx] = 0;
1311 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1312 goto sta;
1313 break;
1314 default:
1315 printf("%s%d: %s illegal %s in state %s\n",
1316 ifp->if_name, ifp->if_unit, cp->name,
1317 sppp_cp_type_name(h->type),
1318 sppp_state_name(sp->state[cp->protoidx]));
1319 ++ifp->if_ierrors;
1320 }
1321 break;
1322 case TERM_ACK:
1323 switch (sp->state[cp->protoidx]) {
1324 case STATE_CLOSED:
1325 case STATE_STOPPED:
1326 case STATE_REQ_SENT:
1327 case STATE_ACK_SENT:
1328 break;
1329 case STATE_CLOSING:
1330 (cp->tlf)(sp);
1331 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1332 break;
1333 case STATE_STOPPING:
1334 (cp->tlf)(sp);
1335 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1336 break;
1337 case STATE_ACK_RCVD:
1338 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1339 break;
1340 case STATE_OPENED:
1341 (cp->tld)(sp);
1342 (cp->scr)(sp);
1343 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1344 break;
1345 default:
1346 printf("%s%d: %s illegal %s in state %s\n",
1347 ifp->if_name, ifp->if_unit, cp->name,
1348 sppp_cp_type_name(h->type),
1349 sppp_state_name(sp->state[cp->protoidx]));
1350 ++ifp->if_ierrors;
1351 }
1352 break;
1353 case CODE_REJ:
1354 case PROTO_REJ:
1355 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1356 log(LOG_INFO,
1357 "%s%d: %s: ignoring RXJ (%s) for proto 0x%x, "
1358 "danger will robinson\n",
1359 ifp->if_name, ifp->if_unit, cp->name,
1360 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1361 switch (sp->state[cp->protoidx]) {
1362 case STATE_CLOSED:
1363 case STATE_STOPPED:
1364 case STATE_REQ_SENT:
1365 case STATE_ACK_SENT:
1366 case STATE_CLOSING:
1367 case STATE_STOPPING:
1368 case STATE_OPENED:
1369 break;
1370 case STATE_ACK_RCVD:
1371 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1372 break;
1373 default:
1374 printf("%s%d: %s illegal %s in state %s\n",
1375 ifp->if_name, ifp->if_unit, cp->name,
1376 sppp_cp_type_name(h->type),
1377 sppp_state_name(sp->state[cp->protoidx]));
1378 ++ifp->if_ierrors;
1379 }
1380 break;
1381 case DISC_REQ:
1382 if (cp->proto != PPP_LCP)
1383 goto illegal;
1384 /* Discard the packet. */
1385 break;
1386 case ECHO_REQ:
1387 if (cp->proto != PPP_LCP)
1388 goto illegal;
1389 if (sp->state[cp->protoidx] != STATE_OPENED) {
1390 if (debug)
1391 addlog("%s%d: lcp echo req but lcp closed\n",
1392 ifp->if_name, ifp->if_unit);
1393 ++ifp->if_ierrors;
1394 break;
1395 }
1396 if (len < 8) {
1397 if (debug)
1398 addlog("%s%d: invalid lcp echo request "
1399 "packet length: %d bytes\n",
1400 ifp->if_name, ifp->if_unit, len);
1401 break;
1402 }
1403 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1404 /* Line loopback mode detected. */
1405 printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1406 if_down (ifp);
1407 sppp_qflush (&sp->pp_cpq);
1408
1409 /* Shut down the PPP link. */
1410 /* XXX */
1411 lcp.Down(sp);
1412 lcp.Up(sp);
1413 break;
1414 }
1415 *(long*)(h+1) = htonl (sp->lcp.magic);
1416 if (debug)
1417 addlog("%s%d: got lcp echo req, sending echo rep\n",
1418 ifp->if_name, ifp->if_unit);
1419 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1420 break;
1421 case ECHO_REPLY:
1422 if (cp->proto != PPP_LCP)
1423 goto illegal;
1424 if (h->ident != sp->lcp.echoid) {
1425 ++ifp->if_ierrors;
1426 break;
1427 }
1428 if (len < 8) {
1429 if (debug)
1430 addlog("%s%d: lcp invalid echo reply "
1431 "packet length: %d bytes\n",
1432 ifp->if_name, ifp->if_unit, len);
1433 break;
1434 }
1435 if (debug)
1436 addlog("%s%d: lcp got echo rep\n",
1437 ifp->if_name, ifp->if_unit);
1438 if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1439 sp->pp_alivecnt = 0;
1440 break;
1441 default:
1442 /* Unknown packet type -- send Code-Reject packet. */
1443 illegal:
1444 if (debug)
1445 addlog("%s%d: %c send code-rej for 0x%x\n",
1446 ifp->if_name, ifp->if_unit, cp->name, h->type);
1447 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1448 m->m_pkthdr.len, h);
1449 ++ifp->if_ierrors;
1450 }
1451}
1452
1453
1454/*
1455 * The generic part of all Up/Down/Open/Close/TO event handlers.
1456 * Basically, the state transition handling in the automaton.
1457 */
1458static void
1459sppp_up_event(const struct cp *cp, struct sppp *sp)
1460{
1461 STDDCL;
1462
1463 if (debug)
1464 log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1465 ifp->if_name, ifp->if_unit, cp->name,
1466 sppp_state_name(sp->state[cp->protoidx]));
1467
1468 switch (sp->state[cp->protoidx]) {
1469 case STATE_INITIAL:
1470 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1471 break;
1472 case STATE_STARTING:
1473 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1474 (cp->scr)(sp);
1475 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1476 break;
1477 default:
1478 printf("%s%d: %s illegal up in state %s\n",
1479 ifp->if_name, ifp->if_unit, cp->name,
1480 sppp_state_name(sp->state[cp->protoidx]));
1481 }
1482}
1483
1484static void
1485sppp_down_event(const struct cp *cp, struct sppp *sp)
1486{
1487 STDDCL;
1488
1489 if (debug)
1490 log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1491 ifp->if_name, ifp->if_unit, cp->name,
1492 sppp_state_name(sp->state[cp->protoidx]));
1493
1494 switch (sp->state[cp->protoidx]) {
1495 case STATE_CLOSED:
1496 case STATE_CLOSING:
1497 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1498 break;
1499 case STATE_STOPPED:
1500 (cp->tls)(sp);
1501 /* fall through */
1502 case STATE_STOPPING:
1503 case STATE_REQ_SENT:
1504 case STATE_ACK_RCVD:
1505 case STATE_ACK_SENT:
1506 sppp_cp_change_state(cp, sp, STATE_STARTING);
1507 break;
1508 case STATE_OPENED:
1509 (cp->tld)(sp);
1510 sppp_cp_change_state(cp, sp, STATE_STARTING);
1511 break;
1512 default:
1513 printf("%s%d: %s illegal down in state %s\n",
1514 ifp->if_name, ifp->if_unit, cp->name,
1515 sppp_state_name(sp->state[cp->protoidx]));
1516 }
1517}
1518
1519
1520static void
1521sppp_open_event(const struct cp *cp, struct sppp *sp)
1522{
1523 STDDCL;
1524
1525 if (debug)
1526 log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1527 ifp->if_name, ifp->if_unit, cp->name,
1528 sppp_state_name(sp->state[cp->protoidx]));
1529
1530 switch (sp->state[cp->protoidx]) {
1531 case STATE_INITIAL:
1532 (cp->tls)(sp);
1533 sppp_cp_change_state(cp, sp, STATE_STARTING);
1534 break;
1535 case STATE_STARTING:
1536 break;
1537 case STATE_CLOSED:
1538 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1539 (cp->scr)(sp);
1540 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1541 break;
1542 case STATE_STOPPED:
1543 case STATE_STOPPING:
1544 case STATE_REQ_SENT:
1545 case STATE_ACK_RCVD:
1546 case STATE_ACK_SENT:
1547 case STATE_OPENED:
1548 break;
1549 case STATE_CLOSING:
1550 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1551 break;
1552 }
1553}
1554
1555
1556static void
1557sppp_close_event(const struct cp *cp, struct sppp *sp)
1558{
1559 STDDCL;
1560
1561 if (debug)
1562 log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1563 ifp->if_name, ifp->if_unit, cp->name,
1564 sppp_state_name(sp->state[cp->protoidx]));
1565
1566 switch (sp->state[cp->protoidx]) {
1567 case STATE_INITIAL:
1568 case STATE_CLOSED:
1569 case STATE_CLOSING:
1570 break;
1571 case STATE_STARTING:
1572 (cp->tlf)(sp);
1573 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1574 break;
1575 case STATE_STOPPED:
1576 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1577 break;
1578 case STATE_STOPPING:
1579 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1580 break;
1581 case STATE_OPENED:
1582 (cp->tld)(sp);
1583 /* fall through */
1584 case STATE_REQ_SENT:
1585 case STATE_ACK_RCVD:
1586 case STATE_ACK_SENT:
1587 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1588 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1589 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1590 break;
1591 }
1592}
1593
1594static void
1595sppp_to_event(const struct cp *cp, struct sppp *sp)
1596{
1597 STDDCL;
1598 int s;
1599
1600 s = splimp();
1601 if (debug)
1602 log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1603 ifp->if_name, ifp->if_unit, cp->name,
1604 sppp_state_name(sp->state[cp->protoidx]),
1605 sp->rst_counter[cp->protoidx]);
1606
1607 if (--sp->rst_counter[cp->protoidx] < 0)
1608 /* TO- event */
1609 switch (sp->state[cp->protoidx]) {
1610 case STATE_CLOSING:
1611 (cp->tlf)(sp);
1612 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1613 break;
1614 case STATE_STOPPING:
1615 (cp->tlf)(sp);
1616 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1617 break;
1618 case STATE_REQ_SENT:
1619 case STATE_ACK_RCVD:
1620 case STATE_ACK_SENT:
1621 (cp->tlf)(sp);
1622 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1623 break;
1624 }
1625 else
1626 /* TO+ event */
1627 switch (sp->state[cp->protoidx]) {
1628 case STATE_CLOSING:
1629 case STATE_STOPPING:
1630 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1631 0, 0);
1632 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1633 sp->lcp.timeout);
1634 break;
1635 case STATE_REQ_SENT:
1636 case STATE_ACK_RCVD:
1637 (cp->scr)(sp);
1638 /* sppp_cp_change_state() will restart the timer */
1639 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1640 break;
1641 case STATE_ACK_SENT:
1642 (cp->scr)(sp);
1643 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1644 sp->lcp.timeout);
1645 break;
1646 }
1647
1648 splx(s);
1649}
1650
1651/*
1652 * Change the state of a control protocol in the state automaton.
1653 * Takes care of starting/stopping the restart timer.
1654 */
1655void
1656sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1657{
1658 sp->state[cp->protoidx] = newstate;
1659
1660 untimeout(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1661 switch (newstate) {
1662 case STATE_INITIAL:
1663 case STATE_STARTING:
1664 case STATE_CLOSED:
1665 case STATE_STOPPED:
1666 case STATE_OPENED:
1667 break;
1668 case STATE_CLOSING:
1669 case STATE_STOPPING:
1670 case STATE_REQ_SENT:
1671 case STATE_ACK_RCVD:
1672 case STATE_ACK_SENT:
1673 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1674 sp->lcp.timeout);
1675 break;
1676 }
1677}
1678/*
1679 *--------------------------------------------------------------------------*
1680 * *
1681 * The LCP implementation. *
1682 * *
1683 *--------------------------------------------------------------------------*
1684 */
1685static void
1686sppp_lcp_init(struct sppp *sp)
1687{
1688 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1689 sp->lcp.magic = 0;
1690 sp->state[IDX_LCP] = STATE_INITIAL;
1691 sp->fail_counter[IDX_LCP] = 0;
1692 sp->lcp.protos = 0;
1693 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1694
1695 /*
1696 * Initialize counters and timeout values. Note that we don't
1697 * use the 3 seconds suggested in RFC 1661 since we are likely
1698 * running on a fast link. XXX We should probably implement
1699 * the exponential backoff option. Note that these values are
1700 * relevant for all control protocols, not just LCP only.
1701 */
1702 sp->lcp.timeout = 1 * hz;
1703 sp->lcp.max_terminate = 2;
1704 sp->lcp.max_configure = 10;
1705 sp->lcp.max_failure = 10;
1706 callout_handle_init(&sp->ch[IDX_LCP]);
1707}
1708
1709static void
1710sppp_lcp_up(struct sppp *sp)
1711{
1712 STDDCL;
1713
1714 /*
1715 * If this interface is passive or dial-on-demand, and we are
1716 * still in Initial state, it means we've got an incoming
1717 * call. Activate the interface.
1718 */
1719 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1720 if (debug)
1721 log(LOG_DEBUG,
1722 "%s%d: Up event", ifp->if_name, ifp->if_unit);
1723 ifp->if_flags |= IFF_RUNNING;
1724 if (sp->state[IDX_LCP] == STATE_INITIAL) {
1725 if (debug)
1726 addlog("(incoming call)\n");
1727 sp->pp_flags |= PP_CALLIN;
1728 lcp.Open(sp);
1729 } else if (debug)
1730 addlog("\n");
1731 }
1732
1733 sppp_up_event(&lcp, sp);
1734}
1735
1736static void
1737sppp_lcp_down(struct sppp *sp)
1738{
1739 STDDCL;
1740
1741 sppp_down_event(&lcp, sp);
1742
1743 /*
1744 * If this is neither a dial-on-demand nor a passive
1745 * interface, simulate an ``ifconfig down'' action, so the
1746 * administrator can force a redial by another ``ifconfig
1747 * up''. XXX For leased line operation, should we immediately
1748 * try to reopen the connection here?
1749 */
1750 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1751 log(LOG_INFO,
1752 "%s%d: Down event (carrier loss), taking interface down.\n",
1753 ifp->if_name, ifp->if_unit);
1754 if_down(ifp);
1755 } else {
1756 if (debug)
1757 log(LOG_DEBUG,
1758 "%s%d: Down event (carrier loss)\n",
1759 ifp->if_name, ifp->if_unit);
1760 }
1761 sp->pp_flags &= ~PP_CALLIN;
1762 if (sp->state[IDX_LCP] != STATE_INITIAL)
1763 lcp.Close(sp);
1764 ifp->if_flags &= ~IFF_RUNNING;
1765}
1766
1767static void
1768sppp_lcp_open(struct sppp *sp)
1769{
1770 /*
1771 * If we are authenticator, negotiate LCP_AUTH
1772 */
1773 if (sp->hisauth.proto != 0)
1774 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1775 else
1776 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1777 sp->pp_flags &= ~PP_NEEDAUTH;
1778 sppp_open_event(&lcp, sp);
1779}
1780
1781static void
1782sppp_lcp_close(struct sppp *sp)
1783{
1784 sppp_close_event(&lcp, sp);
1785}
1786
1787static void
1788sppp_lcp_TO(void *cookie)
1789{
1790 sppp_to_event(&lcp, (struct sppp *)cookie);
1791}
1792
1793/*
1794 * Analyze a configure request. Return true if it was agreeable, and
1795 * caused action sca, false if it has been rejected or nak'ed, and
1796 * caused action scn. (The return value is used to make the state
1797 * transition decision in the state automaton.)
1798 */
1799static int
1800sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1801{
1802 STDDCL;
1803 u_char *buf, *r, *p;
1804 int origlen, rlen;
1805 u_long nmagic;
1806 u_short authproto;
1807
1808 len -= 4;
1809 origlen = len;
1810 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1811 if (! buf)
1812 return (0);
1813
1814 if (debug)
1815 log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1816 ifp->if_name, ifp->if_unit);
1817
1818 /* pass 1: check for things that need to be rejected */
1819 p = (void*) (h+1);
1820 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1821 if (debug)
1822 addlog(" %s ", sppp_lcp_opt_name(*p));
1823 switch (*p) {
1824 case LCP_OPT_MAGIC:
1825 /* Magic number. */
1826 /* fall through, both are same length */
1827 case LCP_OPT_ASYNC_MAP:
1828 /* Async control character map. */
1829 if (len >= 6 || p[1] == 6)
1830 continue;
1831 if (debug)
1832 addlog("[invalid] ");
1833 break;
1834 case LCP_OPT_MRU:
1835 /* Maximum receive unit. */
1836 if (len >= 4 && p[1] == 4)
1837 continue;
1838 if (debug)
1839 addlog("[invalid] ");
1840 break;
1841 case LCP_OPT_AUTH_PROTO:
1842 if (len < 4) {
1843 if (debug)
1844 addlog("[invalid] ");
1845 break;
1846 }
1847 authproto = (p[2] << 8) + p[3];
1848 if (authproto == PPP_CHAP && p[1] != 5) {
1849 if (debug)
1850 addlog("[invalid chap len] ");
1851 break;
1852 }
1853 if (sp->myauth.proto == 0) {
1854 /* we are not configured to do auth */
1855 if (debug)
1856 addlog("[not configured] ");
1857 break;
1858 }
1859 /*
1860 * Remote want us to authenticate, remember this,
1861 * so we stay in PHASE_AUTHENTICATE after LCP got
1862 * up.
1863 */
1864 sp->pp_flags |= PP_NEEDAUTH;
1865 continue;
1866 default:
1867 /* Others not supported. */
1868 if (debug)
1869 addlog("[rej] ");
1870 break;
1871 }
1872 /* Add the option to rejected list. */
1873 bcopy (p, r, p[1]);
1874 r += p[1];
1875 rlen += p[1];
1876 }
1877 if (rlen) {
1878 if (debug)
1879 addlog(" send conf-rej\n");
1880 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1881 return 0;
1882 } else if (debug)
1883 addlog("\n");
1884
1885 /*
1886 * pass 2: check for option values that are unacceptable and
1887 * thus require to be nak'ed.
1888 */
1889 if (debug)
1890 log(LOG_DEBUG, "%s%d: lcp parse opt values: ",
1891 ifp->if_name, ifp->if_unit);
1892
1893 p = (void*) (h+1);
1894 len = origlen;
1895 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1896 if (debug)
1897 addlog(" %s ", sppp_lcp_opt_name(*p));
1898 switch (*p) {
1899 case LCP_OPT_MAGIC:
1900 /* Magic number -- extract. */
1901 nmagic = (u_long)p[2] << 24 |
1902 (u_long)p[3] << 16 | p[4] << 8 | p[5];
1903 if (nmagic != sp->lcp.magic) {
1904 if (debug)
1905 addlog("0x%x ", nmagic);
1906 continue;
1907 }
1908 /*
1909 * Local and remote magics equal -- loopback?
1910 */
1911 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1912 printf ("%s%d: loopback\n",
1913 ifp->if_name, ifp->if_unit);
1914 sp->pp_loopcnt = 0;
1915 if (ifp->if_flags & IFF_UP) {
1916 if_down(ifp);
1917 sppp_qflush(&sp->pp_cpq);
1918 /* XXX ? */
1919 lcp.Down(sp);
1920 lcp.Up(sp);
1921 }
1922 } else if (debug)
1923 addlog("[glitch] ");
1924 ++sp->pp_loopcnt;
1925 /*
1926 * We negate our magic here, and NAK it. If
1927 * we see it later in an NAK packet, we
1928 * suggest a new one.
1929 */
1930 nmagic = ~sp->lcp.magic;
1931 /* Gonna NAK it. */
1932 p[2] = nmagic >> 24;
1933 p[3] = nmagic >> 16;
1934 p[4] = nmagic >> 8;
1935 p[5] = nmagic;
1936 break;
1937
1938 case LCP_OPT_ASYNC_MAP:
1939 /* Async control character map -- check to be zero. */
1940 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1941 if (debug)
1942 addlog("[empty] ");
1943 continue;
1944 }
1945 if (debug)
1946 addlog("[non-empty] ");
1947 /* suggest a zero one */
1948 p[2] = p[3] = p[4] = p[5] = 0;
1949 break;
1950
1951 case LCP_OPT_MRU:
1952 /*
1953 * Maximum receive unit. Always agreeable,
1954 * but ignored by now.
1955 */
1956 sp->lcp.their_mru = p[2] * 256 + p[3];
1957 if (debug)
1958 addlog("%d ", sp->lcp.their_mru);
1959 continue;
1960
1961 case LCP_OPT_AUTH_PROTO:
1962 authproto = (p[2] << 8) + p[3];
1963 if (sp->myauth.proto != authproto) {
1964 /* not agreed, nak */
1965 if (debug)
1966 addlog("[mine %s != his %s] ",
1967 sppp_proto_name(sp->hisauth.proto),
1968 sppp_proto_name(authproto));
1969 p[2] = sp->myauth.proto >> 8;
1970 p[3] = sp->myauth.proto;
1971 break;
1972 }
1973 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
1974 if (debug)
1975 addlog("[chap not MD5] ");
1976 p[4] == CHAP_MD5;
1977 break;
1978 }
1979 continue;
1980 }
1981 /* Add the option to nak'ed list. */
1982 bcopy (p, r, p[1]);
1983 r += p[1];
1984 rlen += p[1];
1985 }
1986 if (rlen) {
1987 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1988 if (debug)
1989 addlog(" max_failure (%d) exceeded, "
1990 "send conf-rej\n",
1991 sp->lcp.max_failure);
1992 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1993 } else {
1994 if (debug)
1995 addlog(" send conf-nak\n");
1996 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1997 }
1998 return 0;
1999 } else {
2000 if (debug)
2001 addlog(" send conf-ack\n");
2002 sp->fail_counter[IDX_LCP] = 0;
2003 sp->pp_loopcnt = 0;
2004 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2005 h->ident, origlen, h+1);
2006 }
2007
2008 free (buf, M_TEMP);
2009 return (rlen == 0);
2010}
2011
2012/*
2013 * Analyze the LCP Configure-Reject option list, and adjust our
2014 * negotiation.
2015 */
2016static void
2017sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2018{
2019 STDDCL;
2020 u_char *buf, *p;
2021
2022 len -= 4;
2023 buf = malloc (len, M_TEMP, M_NOWAIT);
2024 if (!buf)
2025 return;
2026
2027 if (debug)
2028 log(LOG_DEBUG, "%s%d: lcp rej opts: ",
2029 ifp->if_name, ifp->if_unit);
2030
2031 p = (void*) (h+1);
2032 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2033 if (debug)
2034 addlog(" %s ", sppp_lcp_opt_name(*p));
2035 switch (*p) {
2036 case LCP_OPT_MAGIC:
2037 /* Magic number -- can't use it, use 0 */
2038 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2039 sp->lcp.magic = 0;
2040 break;
2041 case LCP_OPT_MRU:
2042 /*
2043 * Should not be rejected anyway, since we only
2044 * negotiate a MRU if explicitly requested by
2045 * peer.
2046 */
2047 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2048 break;
2049 case LCP_OPT_AUTH_PROTO:
2050 /*
2051 * Peer doesn't want to authenticate himself,
2052 * deny unless this is a dialout call, and
2053 * AUTHFLAG_NOCALLOUT is set.
2054 */
2055 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2056 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2057 if (debug)
2058 addlog("[don't insist on auth "
2059 "for callout]");
2060 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2061 break;
2062 }
2063 if (debug)
2064 addlog("[access denied]\n");
2065 lcp.Close(sp);
2066 break;
2067 }
2068 }
2069 if (debug)
2070 addlog("\n");
2071 free (buf, M_TEMP);
2072 return;
2073}
2074
2075/*
2076 * Analyze the LCP Configure-NAK option list, and adjust our
2077 * negotiation.
2078 */
2079static void
2080sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2081{
2082 STDDCL;
2083 u_char *buf, *p;
2084 u_long magic;
2085
2086 len -= 4;
2087 buf = malloc (len, M_TEMP, M_NOWAIT);
2088 if (!buf)
2089 return;
2090
2091 if (debug)
2092 log(LOG_DEBUG, "%s%d: lcp nak opts: ",
2093 ifp->if_name, ifp->if_unit);
2094
2095 p = (void*) (h+1);
2096 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2097 if (debug)
2098 addlog(" %s ", sppp_lcp_opt_name(*p));
2099 switch (*p) {
2100 case LCP_OPT_MAGIC:
2101 /* Magic number -- renegotiate */
2102 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2103 len >= 6 && p[1] == 6) {
2104 magic = (u_long)p[2] << 24 |
2105 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2106 /*
2107 * If the remote magic is our negated one,
2108 * this looks like a loopback problem.
2109 * Suggest a new magic to make sure.
2110 */
2111 if (magic == ~sp->lcp.magic) {
2112 if (debug)
2113 addlog("magic glitch ");
2114 sp->lcp.magic += time.tv_sec + time.tv_usec;
2115 } else {
2116 sp->lcp.magic = magic;
2117 if (debug)
2118 addlog("%d ");
2119 }
2120 }
2121 break;
2122 case LCP_OPT_MRU:
2123 /*
2124 * Peer wants to advise us to negotiate an MRU.
2125 * Agree on it if it's reasonable, or use
2126 * default otherwise.
2127 */
2128 if (len >= 4 && p[1] == 4) {
2129 u_int mru = p[2] * 256 + p[3];
2130 if (debug)
2131 addlog("%d ", mru);
2132 if (mru < PP_MTU || mru > PP_MAX_MRU)
2133 mru = PP_MTU;
2134 sp->lcp.mru = mru;
2135 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2136 }
2137 break;
2138 case LCP_OPT_AUTH_PROTO:
2139 /*
2140 * Peer doesn't like our authentication method,
2141 * deny.
2142 */
2143 if (debug)
2144 addlog("[access denied]\n");
2145 lcp.Close(sp);
2146 break;
2147 }
2148 }
2149 if (debug)
2150 addlog("\n");
2151 free (buf, M_TEMP);
2152 return;
2153}
2154
2155static void
2156sppp_lcp_tlu(struct sppp *sp)
2157{
2158 STDDCL;
2159 int i;
2160 u_long mask;
2161
2162 /* XXX ? */
2163 if (! (ifp->if_flags & IFF_UP) &&
2164 (ifp->if_flags & IFF_RUNNING)) {
2165 /* Coming out of loopback mode. */
2166 if_up(ifp);
2167 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
2168 }
2169
2170 for (i = 0; i < IDX_COUNT; i++)
2171 if ((cps[i])->flags & CP_QUAL)
2172 (cps[i])->Open(sp);
2173
2174 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2175 (sp->pp_flags & PP_NEEDAUTH) != 0)
2176 sp->pp_phase = PHASE_AUTHENTICATE;
2177 else
2178 sp->pp_phase = PHASE_NETWORK;
2179
2180 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2181 sppp_phase_name(sp->pp_phase));
2182
2183 /*
2184 * Open all authentication protocols. This is even required
2185 * if we already proceeded to network phase, since it might be
2186 * that remote wants us to authenticate, so we might have to
2187 * send a PAP request. Undesired authentication protocols
2188 * don't do anything when they get an Open event.
2189 */
2190 for (i = 0; i < IDX_COUNT; i++)
2191 if ((cps[i])->flags & CP_AUTH)
2192 (cps[i])->Open(sp);
2193
2194 if (sp->pp_phase == PHASE_NETWORK) {
2195 /* Notify all NCPs. */
2196 for (i = 0; i < IDX_COUNT; i++)
2197 if ((cps[i])->flags & CP_NCP)
2198 (cps[i])->Open(sp);
2199 }
2200
2201 /* Send Up events to all started protos. */
2202 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2203 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2204 (cps[i])->Up(sp);
2205
2206 if (sp->pp_phase == PHASE_NETWORK)
2207 /* if no NCP is starting, close down */
2208 sppp_lcp_check_and_close(sp);
2209}
2210
2211static void
2212sppp_lcp_tld(struct sppp *sp)
2213{
2214 STDDCL;
2215 int i;
2216 u_long mask;
2217
2218 sp->pp_phase = PHASE_TERMINATE;
2219
2220 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2221 sppp_phase_name(sp->pp_phase));
2222
2223 /*
2224 * Take upper layers down. We send the Down event first and
2225 * the Close second to prevent the upper layers from sending
2226 * ``a flurry of terminate-request packets'', as the RFC
2227 * describes it.
2228 */
2229 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2230 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2231 (cps[i])->Down(sp);
2232 (cps[i])->Close(sp);
2233 }
2234}
2235
2236static void
2237sppp_lcp_tls(struct sppp *sp)
2238{
2239 STDDCL;
2240
2241 sp->pp_phase = PHASE_ESTABLISH;
2242
2243 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2244 sppp_phase_name(sp->pp_phase));
2245
2246 /* Notify lower layer if desired. */
2247 if (sp->pp_tls)
2248 (sp->pp_tls)(sp);
2249}
2250
2251static void
2252sppp_lcp_tlf(struct sppp *sp)
2253{
2254 STDDCL;
2255
2256 sp->pp_phase = PHASE_DEAD;
2257 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2258 sppp_phase_name(sp->pp_phase));
2259
2260 /* Notify lower layer if desired. */
2261 if (sp->pp_tlf)
2262 (sp->pp_tlf)(sp);
2263}
2264
2265static void
2266sppp_lcp_scr(struct sppp *sp)
2267{
2268 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2269 int i = 0;
2270 u_short authproto;
2271
2272 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2273 if (! sp->lcp.magic)
2274 sp->lcp.magic = time.tv_sec + time.tv_usec;
2275 opt[i++] = LCP_OPT_MAGIC;
2276 opt[i++] = 6;
2277 opt[i++] = sp->lcp.magic >> 24;
2278 opt[i++] = sp->lcp.magic >> 16;
2279 opt[i++] = sp->lcp.magic >> 8;
2280 opt[i++] = sp->lcp.magic;
2281 }
2282
2283 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2284 opt[i++] = LCP_OPT_MRU;
2285 opt[i++] = 4;
2286 opt[i++] = sp->lcp.mru >> 8;
2287 opt[i++] = sp->lcp.mru;
2288 }
2289
2290 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2291 authproto = sp->hisauth.proto;
2292 opt[i++] = LCP_OPT_AUTH_PROTO;
2293 opt[i++] = authproto == PPP_CHAP? 5: 4;
2294 opt[i++] = authproto >> 8;
2295 opt[i++] = authproto;
2296 if (authproto == PPP_CHAP)
2297 opt[i++] = CHAP_MD5;
2298 }
2299
2300 sp->confid[IDX_LCP] = ++sp->pp_seq;
2301 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2302}
2303
2304/*
2305 * Check the open NCPs, return true if at least one NCP is open.
2306 */
2307static int
2308sppp_ncp_check(struct sppp *sp)
2309{
2310 int i, mask;
2311
2312 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2313 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2314 return 1;
2315 return 0;
2316}
2317
2318/*
2319 * Re-check the open NCPs and see if we should terminate the link.
2320 * Called by the NCPs during their tlf action handling.
2321 */
2322static void
2323sppp_lcp_check_and_close(struct sppp *sp)
2324{
2325
2326 if (sp->pp_phase < PHASE_NETWORK)
2327 /* don't bother, we are already going down */
2328 return;
2329
2330 if (sppp_ncp_check(sp))
2331 return;
2332
2333 lcp.Close(sp);
2334}
2335/*
2336 *--------------------------------------------------------------------------*
2337 * *
2338 * The IPCP implementation. *
2339 * *
2340 *--------------------------------------------------------------------------*
2341 */
2342
2343static void
2344sppp_ipcp_init(struct sppp *sp)
2345{
2346 sp->ipcp.opts = 0;
2347 sp->ipcp.flags = 0;
2348 sp->state[IDX_IPCP] = STATE_INITIAL;
2349 sp->fail_counter[IDX_IPCP] = 0;
2350 callout_handle_init(&sp->ch[IDX_IPCP]);
2351}
2352
2353static void
2354sppp_ipcp_up(struct sppp *sp)
2355{
2356 sppp_up_event(&ipcp, sp);
2357}
2358
2359static void
2360sppp_ipcp_down(struct sppp *sp)
2361{
2362 sppp_down_event(&ipcp, sp);
2363}
2364
2365static void
2366sppp_ipcp_open(struct sppp *sp)
2367{
2368 STDDCL;
2369 u_long myaddr, hisaddr;
2370
2371 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2372 /*
2373 * If we don't have his address, this probably means our
2374 * interface doesn't want to talk IP at all. (This could
2375 * be the case if somebody wants to speak only IPX, for
2376 * example.) Don't open IPCP in this case.
2377 */
2378 if (hisaddr == 0L) {
2379 /* XXX this message should go away */
2380 if (debug)
2381 log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2382 ifp->if_name, ifp->if_unit);
2383 return;
2384 }
2385
2386 if (myaddr == 0L) {
2387 /*
2388 * I don't have an assigned address, so i need to
2389 * negotiate my address.
2390 */
2391 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2392 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2393 }
2394 sppp_open_event(&ipcp, sp);
2395}
2396
2397static void
2398sppp_ipcp_close(struct sppp *sp)
2399{
2400 sppp_close_event(&ipcp, sp);
2401 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2402 /*
2403 * My address was dynamic, clear it again.
2404 */
2405 sppp_set_ip_addr(sp, 0L);
2406}
2407
2408static void
2409sppp_ipcp_TO(void *cookie)
2410{
2411 sppp_to_event(&ipcp, (struct sppp *)cookie);
2412}
2413
2414/*
2415 * Analyze a configure request. Return true if it was agreeable, and
2416 * caused action sca, false if it has been rejected or nak'ed, and
2417 * caused action scn. (The return value is used to make the state
2418 * transition decision in the state automaton.)
2419 */
2420static int
2421sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2422{
2423 u_char *buf, *r, *p;
2424 struct ifnet *ifp = &sp->pp_if;
2425 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2426 u_long hisaddr, desiredaddr;
2427
2428 len -= 4;
2429 origlen = len;
2430 /*
2431 * Make sure to allocate a buf that can at least hold a
2432 * conf-nak with an `address' option. We might need it below.
2433 */
2434 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2435 if (! buf)
2436 return (0);
2437
2438 /* pass 1: see if we can recognize them */
2439 if (debug)
2440 log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2441 ifp->if_name, ifp->if_unit);
2442 p = (void*) (h+1);
2443 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2444 if (debug)
2445 addlog(" %s ", sppp_ipcp_opt_name(*p));
2446 switch (*p) {
2447#ifdef notyet
2448 case IPCP_OPT_COMPRESSION:
2449 if (len >= 6 && p[1] >= 6) {
2450 /* correctly formed compress option */
2451 continue;
2452 }
2453 if (debug)
2454 addlog("[invalid] ");
2455 break;
2456#endif
2457 case IPCP_OPT_ADDRESS:
2458 if (len >= 6 && p[1] == 6) {
2459 /* correctly formed address option */
2460 continue;
2461 }
2462 if (debug)
2463 addlog("[invalid] ");
2464 break;
2465 default:
2466 /* Others not supported. */
2467 if (debug)
2468 addlog("[rej] ");
2469 break;
2470 }
2471 /* Add the option to rejected list. */
2472 bcopy (p, r, p[1]);
2473 r += p[1];
2474 rlen += p[1];
2475 }
2476 if (rlen) {
2477 if (debug)
2478 addlog(" send conf-rej\n");
2479 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2480 return 0;
2481 } else if (debug)
2482 addlog("\n");
2483
2484 /* pass 2: parse option values */
2485 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2486 if (debug)
2487 log(LOG_DEBUG, "%s%d: ipcp parse opt values: ",
2488 ifp->if_name, ifp->if_unit);
2489 p = (void*) (h+1);
2490 len = origlen;
2491 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2492 if (debug)
2493 addlog(" %s ", sppp_ipcp_opt_name(*p));
2494 switch (*p) {
2495#ifdef notyet
2496 case IPCP_OPT_COMPRESSION:
2497 continue;
2498#endif
2499 case IPCP_OPT_ADDRESS:
2500 desiredaddr = p[2] << 24 | p[3] << 16 |
2501 p[4] << 8 | p[5];
2502 if (desiredaddr == hisaddr) {
2503 /*
2504 * Peer's address is same as our value,
2505 * this is agreeable. Gonna conf-ack
2506 * it.
2507 */
2508 if (debug)
2509 addlog("%s [ack] ",
2510 sppp_dotted_quad(hisaddr));
2511 /* record that we've seen it already */
2512 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2513 continue;
2514 }
2515 /*
2516 * The address wasn't agreeable. This is either
2517 * he sent us 0.0.0.0, asking to assign him an
2518 * address, or he send us another address not
2519 * matching our value. Either case, we gonna
2520 * conf-nak it with our value.
2521 */
2522 if (debug) {
2523 if (desiredaddr == 0)
2524 addlog("[addr requested] ");
2525 else
2526 addlog("%s [not agreed] ",
2527 sppp_dotted_quad(desiredaddr));
2528
2529 p[2] = hisaddr >> 24;
2530 p[3] = hisaddr >> 16;
2531 p[4] = hisaddr >> 8;
2532 p[5] = hisaddr;
2533 }
2534 break;
2535 }
2536 /* Add the option to nak'ed list. */
2537 bcopy (p, r, p[1]);
2538 r += p[1];
2539 rlen += p[1];
2540 }
2541
2542 /*
2543 * If we are about to conf-ack the request, but haven't seen
2544 * his address so far, gonna conf-nak it instead, with the
2545 * `address' option present and our idea of his address being
2546 * filled in there, to request negotiation of both addresses.
2547 *
2548 * XXX This can result in an endless req - nak loop if peer
2549 * doesn't want to send us his address. Q: What should we do
2550 * about it? XXX A: implement the max-failure counter.
2551 */
2552 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2553 buf[0] = IPCP_OPT_ADDRESS;
2554 buf[1] = 6;
2555 buf[2] = hisaddr >> 24;
2556 buf[3] = hisaddr >> 16;
2557 buf[4] = hisaddr >> 8;
2558 buf[5] = hisaddr;
2559 rlen = 6;
2560 if (debug)
2561 addlog("still need hisaddr ");
2562 }
2563
2564 if (rlen) {
2565 if (debug)
2566 addlog(" send conf-nak\n");
2567 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2568 } else {
2569 if (debug)
2570 addlog(" send conf-ack\n");
2571 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2572 h->ident, origlen, h+1);
2573 }
2574
2575 free (buf, M_TEMP);
2576 return (rlen == 0);
2577}
2578
2579/*
2580 * Analyze the IPCP Configure-Reject option list, and adjust our
2581 * negotiation.
2582 */
2583static void
2584sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2585{
2586 u_char *buf, *p;
2587 struct ifnet *ifp = &sp->pp_if;
2588 int debug = ifp->if_flags & IFF_DEBUG;
2589
2590 len -= 4;
2591 buf = malloc (len, M_TEMP, M_NOWAIT);
2592 if (!buf)
2593 return;
2594
2595 if (debug)
2596 log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2597 ifp->if_name, ifp->if_unit);
2598
2599 p = (void*) (h+1);
2600 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2601 if (debug)
2602 addlog(" %s ", sppp_ipcp_opt_name(*p));
2603 switch (*p) {
2604 case IPCP_OPT_ADDRESS:
2605 /*
2606 * Peer doesn't grok address option. This is
2607 * bad. XXX Should we better give up here?
2608 */
2609 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2610 break;
2611#ifdef notyet
2612 case IPCP_OPT_COMPRESS:
2613 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2614 break;
2615#endif
2616 }
2617 }
2618 if (debug)
2619 addlog("\n");
2620 free (buf, M_TEMP);
2621 return;
2622}
2623
2624/*
2625 * Analyze the IPCP Configure-NAK option list, and adjust our
2626 * negotiation.
2627 */
2628static void
2629sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2630{
2631 u_char *buf, *p;
2632 struct ifnet *ifp = &sp->pp_if;
2633 int debug = ifp->if_flags & IFF_DEBUG;
2634 u_long wantaddr;
2635
2636 len -= 4;
2637 buf = malloc (len, M_TEMP, M_NOWAIT);
2638 if (!buf)
2639 return;
2640
2641 if (debug)
2642 log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2643 ifp->if_name, ifp->if_unit);
2644
2645 p = (void*) (h+1);
2646 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2647 if (debug)
2648 addlog(" %s ", sppp_ipcp_opt_name(*p));
2649 switch (*p) {
2650 case IPCP_OPT_ADDRESS:
2651 /*
2652 * Peer doesn't like our local IP address. See
2653 * if we can do something for him. We'll drop
2654 * him our address then.
2655 */
2656 if (len >= 6 && p[1] == 6) {
2657 wantaddr = p[2] << 24 | p[3] << 16 |
2658 p[4] << 8 | p[5];
2659 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2660 if (debug)
2661 addlog("[wantaddr %s] ",
2662 sppp_dotted_quad(wantaddr));
2663 /*
2664 * When doing dynamic address assignment,
2665 * we accept his offer. Otherwise, we
2666 * ignore it and thus continue to negotiate
2667 * our already existing value.
2668 */
2669 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2670 sppp_set_ip_addr(sp, wantaddr);
2671 if (debug)
2672 addlog("[agree] ");
2673 }
2674 }
2675 break;
2676#ifdef notyet
2677 case IPCP_OPT_COMPRESS:
2678 /*
2679 * Peer wants different compression parameters.
2680 */
2681 break;
2682#endif
2683 }
2684 }
2685 if (debug)
2686 addlog("\n");
2687 free (buf, M_TEMP);
2688 return;
2689}
2690
2691static void
2692sppp_ipcp_tlu(struct sppp *sp)
2693{
2694}
2695
2696static void
2697sppp_ipcp_tld(struct sppp *sp)
2698{
2699}
2700
2701static void
2702sppp_ipcp_tls(struct sppp *sp)
2703{
2704 /* indicate to LCP that it must stay alive */
2705 sp->lcp.protos |= (1 << IDX_IPCP);
2706}
2707
2708static void
2709sppp_ipcp_tlf(struct sppp *sp)
2710{
2711 /* we no longer need LCP */
2712 sp->lcp.protos &= ~(1 << IDX_IPCP);
2713 sppp_lcp_check_and_close(sp);
2714}
2715
2716static void
2717sppp_ipcp_scr(struct sppp *sp)
2718{
2719 char opt[6 /* compression */ + 6 /* address */];
2720 u_long ouraddr;
2721 int i = 0;
2722
2723#ifdef notyet
2724 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2725 opt[i++] = IPCP_OPT_COMPRESSION;
2726 opt[i++] = 6;
2727 opt[i++] = 0; /* VJ header compression */
2728 opt[i++] = 0x2d; /* VJ header compression */
2729 opt[i++] = max_slot_id;
2730 opt[i++] = comp_slot_id;
2731 }
2732#endif
2733
2734 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2735 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2736 opt[i++] = IPCP_OPT_ADDRESS;
2737 opt[i++] = 6;
2738 opt[i++] = ouraddr >> 24;
2739 opt[i++] = ouraddr >> 16;
2740 opt[i++] = ouraddr >> 8;
2741 opt[i++] = ouraddr;
2742 }
2743
2744 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2745 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2746}
2747
2748
2749/*
2750 *--------------------------------------------------------------------------*
2751 * *
2752 * The CHAP implementation. *
2753 * *
2754 *--------------------------------------------------------------------------*
2755 */
2756
2757/*
2758 * The authentication protocols don't employ a full-fledged state machine as
2759 * the control protocols do, since they do have Open and Close events, but
2760 * not Up and Down, nor are they explicitly terminated. Also, use of the
2761 * authentication protocols may be different in both directions (this makes
2762 * sense, think of a machine that never accepts incoming calls but only
2763 * calls out, it doesn't require the called party to authenticate itself).
2764 *
2765 * Our state machine for the local authentication protocol (we are requesting
2766 * the peer to authenticate) looks like:
2767 *
2768 * RCA-
2769 * +--------------------------------------------+
2770 * V scn,tld|
2771 * +--------+ Close +---------+ RCA+
2772 * | |<----------------------------------| |------+
2773 * +--->| Closed | TO* | Opened | sca |
2774 * | | |-----+ +-------| |<-----+
2775 * | +--------+ irc | | +---------+
2776 * | ^ | | ^
2777 * | | | | |
2778 * | | | | |
2779 * | TO-| | | |
2780 * | |tld TO+ V | |
2781 * | | +------->+ | |
2782 * | | | | | |
2783 * | +--------+ V | |
2784 * | | |<----+<--------------------+ |
2785 * | | Req- | scr |
2786 * | | Sent | |
2787 * | | | |
2788 * | +--------+ |
2789 * | RCA- | | RCA+ |
2790 * +------+ +------------------------------------------+
2791 * scn,tld sca,irc,ict,tlu
2792 *
2793 *
2794 * with:
2795 *
2796 * Open: LCP reached authentication phase
2797 * Close: LCP reached terminate phase
2798 *
2799 * RCA+: received reply (pap-req, chap-response), acceptable
2800 * RCN: received reply (pap-req, chap-response), not acceptable
2801 * TO+: timeout with restart counter >= 0
2802 * TO-: timeout with restart counter < 0
2803 * TO*: reschedule timeout for CHAP
2804 *
2805 * scr: send request packet (none for PAP, chap-challenge)
2806 * sca: send ack packet (pap-ack, chap-success)
2807 * scn: send nak packet (pap-nak, chap-failure)
2808 * ict: initialize re-challenge timer (CHAP only)
2809 *
2810 * tlu: this-layer-up, LCP reaches network phase
2811 * tld: this-layer-down, LCP enters terminate phase
2812 *
2813 * Note that in CHAP mode, after sending a new challenge, while the state
2814 * automaton falls back into Req-Sent state, it doesn't signal a tld
2815 * event to LCP, so LCP remains in network phase. Only after not getting
2816 * any response (or after getting an unacceptable response), CHAP closes,
2817 * causing LCP to enter terminate phase.
2818 *
2819 * With PAP, there is no initial request that can be sent. The peer is
2820 * expected to send one based on the successful negotiation of PAP as
2821 * the authentication protocol during the LCP option negotiation.
2822 *
2823 * Incoming authentication protocol requests (remote requests
2824 * authentication, we are peer) don't employ a state machine at all,
2825 * they are simply answered. Some peers [Ascend P50 firmware rev
2826 * 4.50] react allergically when sending IPCP requests while they are
2827 * still in authentication phase (thereby violating the standard that
2828 * demands that these NCP packets are to be discarded), so we keep
2829 * track of the peer demanding us to authenticate, and only proceed to
2830 * phase network once we've seen a positive acknowledge for the
2831 * authentication.
2832 */
2833
2834/*
2835 * Handle incoming CHAP packets.
2836 */
2837void
2838sppp_chap_input(struct sppp *sp, struct mbuf *m)
2839{
2840 STDDCL;
2841 struct lcp_header *h;
2842 int len, x;
2843 u_char *value, *name, digest[AUTHKEYLEN], dsize;
2844 int value_len, name_len;
2845 MD5_CTX ctx;
2846
2847 len = m->m_pkthdr.len;
2848 if (len < 4) {
2849 if (debug)
2850 log(LOG_DEBUG,
2851 "%s%d: chap invalid packet length: %d bytes\n",
2852 ifp->if_name, ifp->if_unit, len);
2853 return;
2854 }
2855 h = mtod (m, struct lcp_header*);
2856 if (len > ntohs (h->len))
2857 len = ntohs (h->len);
2858
2859 switch (h->type) {
2860 /* challenge, failure and success are his authproto */
2861 case CHAP_CHALLENGE:
2862 value = 1 + (u_char*)(h+1);
2863 value_len = value[-1];
2864 name = value + value_len;
2865 name_len = len - value_len - 5;
2866 if (name_len < 0) {
2867 if (debug) {
2868 log(LOG_DEBUG,
2869 "%s%d: chap corrupted challenge "
2870 "<%s id=0x%x len=%d",
2871 ifp->if_name, ifp->if_unit,
2872 sppp_auth_type_name(PPP_CHAP, h->type),
2873 h->ident, ntohs(h->len));
2874 if (len > 4)
2875 sppp_print_bytes((u_char*) (h+1), len-4);
2876 addlog(">\n");
2877 }
2878 break;
2879 }
2880
2881 if (debug) {
2882 log(LOG_DEBUG,
2883 "%s%d: chap input <%s id=0x%x len=%d name=",
2884 ifp->if_name, ifp->if_unit,
2885 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
2886 ntohs(h->len));
2887 sppp_print_string((char*) name, name_len);
2888 addlog(" value-size=%d value=", value_len);
2889 sppp_print_bytes(value, value_len);
2890 addlog(">\n");
2891 }
2892
2893 /* Compute reply value. */
2894 MD5Init(&ctx);
2895 MD5Update(&ctx, &h->ident, 1);
2896 MD5Update(&ctx, sp->myauth.secret,
2897 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
2898 MD5Update(&ctx, value, value_len);
2899 MD5Final(digest, &ctx);
2900 dsize = sizeof digest;
2901
2902 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
2903 sizeof dsize, (const char *)&dsize,
2904 sizeof digest, digest,
2905 sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
2906 sp->myauth.name,
2907 0);
2908 break;
2909
2910 case CHAP_SUCCESS:
2911 if (debug) {
2912 log(LOG_DEBUG, "%s%d: chap success",
2913 ifp->if_name, ifp->if_unit);
2914 if (len > 4) {
2915 addlog(": ");
2916 sppp_print_string((char*)(h + 1), len - 4);
2917 }
2918 addlog("\n");
2919 }
2920 x = splimp();
2921 sp->pp_flags &= ~PP_NEEDAUTH;
2922 if (sp->myauth.proto == PPP_CHAP &&
2923 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
2924 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
2925 /*
2926 * We are authenticator for CHAP but didn't
2927 * complete yet. Leave it to tlu to proceed
2928 * to network phase.
2929 */
2930 splx(x);
2931 break;
2932 }
2933 splx(x);
2934 sppp_phase_network(sp);
2935 break;
2936
2937 case CHAP_FAILURE:
2938 if (debug) {
2939 log(LOG_INFO, "%s%d: chap failure",
2940 ifp->if_name, ifp->if_unit);
2941 if (len > 4) {
2942 addlog(": ");
2943 sppp_print_string((char*)(h + 1), len - 4);
2944 }
2945 addlog("\n");
2946 } else
2947 log(LOG_INFO, "%s%d: chap failure\n",
2948 ifp->if_name, ifp->if_unit);
2949 /* await LCP shutdown by authenticator */
2950 break;
2951
2952 /* response is my authproto */
2953 case CHAP_RESPONSE:
2954 value = 1 + (u_char*)(h+1);
2955 value_len = value[-1];
2956 name = value + value_len;
2957 name_len = len - value_len - 5;
2958 if (name_len < 0) {
2959 if (debug) {
2960 log(LOG_DEBUG,
2961 "%s%d: chap corrupted response "
2962 "<%s id=0x%x len=%d",
2963 ifp->if_name, ifp->if_unit,
2964 sppp_auth_type_name(PPP_CHAP, h->type),
2965 h->ident, ntohs(h->len));
2966 if (len > 4)
2967 sppp_print_bytes((u_char*)(h+1), len-4);
2968 addlog(">\n");
2969 }
2970 break;
2971 }
2972 if (h->ident != sp->confid[IDX_CHAP]) {
2973 if (debug)
2974 log(LOG_DEBUG,
2975 "%s%d: chap dropping response for old ID "
2976 "(got %d, expected %d)\n",
2977 h->ident, sp->confid[IDX_CHAP]);
2978 break;
2979 }
2980 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
2981 || bcmp(name, sp->hisauth.name, name_len) != 0) {
2982 log(LOG_INFO, "%s%d: chap response, his name ",
2983 ifp->if_name, ifp->if_unit);
2984 sppp_print_string(name, name_len);
2985 addlog(" != expected ");
2986 sppp_print_string(sp->hisauth.name,
2987 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
2988 addlog("\n");
2989 }
2990 if (debug) {
2991 log(LOG_DEBUG, "%s%d: chap input(%s) "
2992 "<%s id=0x%x len=%d name=",
2993 ifp->if_name, ifp->if_unit,
2994 sppp_state_name(sp->state[IDX_CHAP]),
2995 sppp_auth_type_name(PPP_CHAP, h->type),
2996 h->ident, ntohs (h->len));
2997 sppp_print_string((char*)name, name_len);
2998 addlog(" value-size=%d value=", value_len);
2999 sppp_print_bytes(value, value_len);
3000 addlog(">\n");
3001 }
3002 if (value_len != AUTHKEYLEN) {
3003 if (debug)
3004 log(LOG_DEBUG,
3005 "%s%d: chap bad hash value length: "
3006 "%d bytes, should be %d\n",
3007 ifp->if_name, ifp->if_unit, value_len,
3008 AUTHKEYLEN);
3009 break;
3010 }
3011
3012 MD5Init(&ctx);
3013 MD5Update(&ctx, &h->ident, 1);
3014 MD5Update(&ctx, sp->hisauth.secret,
3015 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3016 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3017 MD5Final(digest, &ctx);
3018
3019#define FAILMSG "Failed..."
3020#define SUCCMSG "Welcome!"
3021
3022 if (value_len != sizeof digest ||
3023 bcmp(digest, value, value_len) != 0) {
3024 /* action scn, tld */
3025 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3026 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3027 0);
3028 chap.tld(sp);
3029 break;
3030 }
3031 /* action sca, perhaps tlu */
3032 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3033 sp->state[IDX_CHAP] == STATE_OPENED)
3034 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3035 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3036 0);
3037 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3038 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3039 chap.tlu(sp);
3040 }
3041 break;
3042
3043 default:
3044 /* Unknown CHAP packet type -- ignore. */
3045 if (debug) {
3046 log(LOG_DEBUG, "%s%d: chap unknown input(%s) "
3047 "<0x%x id=0x%xh len=%d",
3048 ifp->if_name, ifp->if_unit,
3049 sppp_state_name(sp->state[IDX_CHAP]),
3050 h->type, h->ident, ntohs(h->len));
3051 if (len > 4)
3052 sppp_print_bytes((u_char*)(h+1), len-4);
3053 addlog(">\n");
3054 }
3055 break;
3056
3057 }
3058}
3059
3060static void
3061sppp_chap_init(struct sppp *sp)
3062{
3063 /* Chap doesn't have STATE_INITIAL at all. */
3064 sp->state[IDX_CHAP] = STATE_CLOSED;
3065 sp->fail_counter[IDX_CHAP] = 0;
3066 callout_handle_init(&sp->ch[IDX_CHAP]);
3067}
3068
3069static void
3070sppp_chap_open(struct sppp *sp)
3071{
3072 if (sp->myauth.proto == PPP_CHAP &&
3073 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3074 /* we are authenticator for CHAP, start it */
3075 chap.scr(sp);
3076 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3077 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3078 }
3079 /* nothing to be done if we are peer, await a challenge */
3080}
3081
3082static void
3083sppp_chap_close(struct sppp *sp)
3084{
3085 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3086 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3087}
3088
3089static void
3090sppp_chap_TO(void *cookie)
3091{
3092 struct sppp *sp = (struct sppp *)cookie;
3093 STDDCL;
3094 int s;
3095
3096 s = splimp();
3097 if (debug)
3098 log(LOG_DEBUG, "%s%d: chap TO(%s) rst_counter = %d\n",
3099 ifp->if_name, ifp->if_unit,
3100 sppp_state_name(sp->state[IDX_CHAP]),
3101 sp->rst_counter[IDX_CHAP]);
3102
3103 if (--sp->rst_counter[IDX_CHAP] < 0)
3104 /* TO- event */
3105 switch (sp->state[IDX_CHAP]) {
3106 case STATE_REQ_SENT:
3107 chap.tld(sp);
3108 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3109 break;
3110 }
3111 else
3112 /* TO+ (or TO*) event */
3113 switch (sp->state[IDX_CHAP]) {
3114 case STATE_OPENED:
3115 /* TO* event */
3116 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3117 /* fall through */
3118 case STATE_REQ_SENT:
3119 chap.scr(sp);
3120 /* sppp_cp_change_state() will restart the timer */
3121 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3122 break;
3123 }
3124
3125 splx(s);
3126}
3127
3128static void
3129sppp_chap_tlu(struct sppp *sp)
3130{
3131 STDDCL;
3132 int i, x;
3133
3134 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3135
3136 /*
3137 * Some broken CHAP implementations (Conware CoNet, firmware
3138 * 4.0.?) don't want to re-authenticate their CHAP once the
3139 * initial challenge-response exchange has taken place.
3140 * Provide for an option to avoid rechallenges.
3141 */
3142 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3143 /*
3144 * Compute the re-challenge timeout. This will yield
3145 * a number between 300 and 810 seconds.
3146 */
3147 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3148
3149 sp->ch[IDX_CHAP] = timeout(chap.TO, (void *)sp, i * hz);
3150 }
3151
3152 if (debug) {
3153 log(LOG_DEBUG,
3154 "%s%d: chap %s, ",
3155 ifp->if_name, ifp->if_unit,
3156 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3157 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3158 addlog("next re-challenge in %d seconds\n", i);
3159 else
3160 addlog("re-challenging supressed\n");
3161 }
3162
3163 x = splimp();
3164 /* indicate to LCP that we need to be closed down */
3165 sp->lcp.protos |= (1 << IDX_CHAP);
3166
3167 if (sp->pp_flags & PP_NEEDAUTH) {
3168 /*
3169 * Remote is authenticator, but his auth proto didn't
3170 * complete yet. Defer the transition to network
3171 * phase.
3172 */
3173 splx(x);
3174 return;
3175 }
3176 splx(x);
3177
3178 /*
3179 * If we are already in phase network, we are done here. This
3180 * is the case if this is a dummy tlu event after a re-challenge.
3181 */
3182 if (sp->pp_phase != PHASE_NETWORK)
3183 sppp_phase_network(sp);
3184}
3185
3186static void
3187sppp_chap_tld(struct sppp *sp)
3188{
3189 STDDCL;
3190
3191 if (debug)
3192 log(LOG_DEBUG, "%s%d: chap tld\n", ifp->if_name, ifp->if_unit);
3193 untimeout(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3194 sp->lcp.protos &= ~(1 << IDX_CHAP);
3195
3196 lcp.Close(sp);
3197}
3198
3199static void
3200sppp_chap_scr(struct sppp *sp)
3201{
3202 struct timeval tv;
3203 u_long *ch, seed;
3204 u_char clen;
3205
3206 /* Compute random challenge. */
3207 ch = (u_long *)sp->myauth.challenge;
3208 microtime(&tv);
3209 seed = tv.tv_sec ^ tv.tv_usec;
3210 ch[0] = seed ^ random();
3211 ch[1] = seed ^ random();
3212 ch[2] = seed ^ random();
3213 ch[3] = seed ^ random();
3214 clen = AUTHKEYLEN;
3215
3216 sp->confid[IDX_CHAP] = ++sp->pp_seq;
3217
3218 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3219 sizeof clen, (const char *)&clen,
3220 AUTHKEYLEN, sp->myauth.challenge,
3221 sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3222 sp->myauth.name,
3223 0);
3224}
3225/*
3226 *--------------------------------------------------------------------------*
3227 * *
3228 * The PAP implementation. *
3229 * *
3230 *--------------------------------------------------------------------------*
3231 */
3232/*
3233 * For PAP, we need to keep a little state also if we are the peer, not the
3234 * authenticator. This is since we don't get a request to authenticate, but
3235 * have to repeatedly authenticate ourself until we got a response (or the
3236 * retry counter is expired).
3237 */
3238
3239/*
3240 * Handle incoming PAP packets. */
3241static void
3242sppp_pap_input(struct sppp *sp, struct mbuf *m)
3243{
3244 STDDCL;
3245 struct lcp_header *h;
3246 int len, x;
3247 u_char *name, *passwd, mlen;
3248 int name_len, passwd_len;
3249
3250 len = m->m_pkthdr.len;
3251 if (len < 5) {
3252 if (debug)
3253 log(LOG_DEBUG,
3254 "%s%d: pap invalid packet length: %d bytes\n",
3255 ifp->if_name, ifp->if_unit, len);
3256 return;
3257 }
3258 h = mtod (m, struct lcp_header*);
3259 if (len > ntohs (h->len))
3260 len = ntohs (h->len);
3261 switch (h->type) {
3262 /* PAP request is my authproto */
3263 case PAP_REQ:
3264 name = 1 + (u_char*)(h+1);
3265 name_len = name[-1];
3266 passwd = name + name_len + 1;
3267 if (name_len > len - 6 ||
3268 (passwd_len = passwd[-1]) > len - 6 - name_len) {
3269 if (debug) {
3270 log(LOG_DEBUG, "%s%d: pap corrupted input "
3271 "<%s id=0x%x len=%d",
3272 ifp->if_name, ifp->if_unit,
3273 sppp_auth_type_name(PPP_PAP, h->type),
3274 h->ident, ntohs(h->len));
3275 if (len > 4)
3276 sppp_print_bytes((u_char*)(h+1), len-4);
3277 addlog(">\n");
3278 }
3279 break;
3280 }
3281 if (debug) {
3282 log(LOG_DEBUG, "%s%d: pap input(%s) "
3283 "<%s id=0x%x len=%d name=",
3284 ifp->if_name, ifp->if_unit,
3285 sppp_state_name(sp->state[IDX_PAP]),
3286 sppp_auth_type_name(PPP_PAP, h->type),
3287 h->ident, ntohs(h->len));
3288 sppp_print_string((char*)name, name_len);
3289 addlog(" passwd=");
3290 sppp_print_string((char*)passwd, passwd_len);
3291 addlog(">\n");
3292 }
3293 if (name_len > AUTHNAMELEN ||
3294 passwd_len > AUTHKEYLEN ||
3295 bcmp(name, sp->hisauth.name, name_len) != 0 ||
3296 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3297 /* action scn, tld */
3298 mlen = sizeof(FAILMSG) - 1;
3299 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3300 sizeof mlen, (const char *)&mlen,
3301 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3302 0);
3303 pap.tld(sp);
3304 break;
3305 }
3306 /* action sca, perhaps tlu */
3307 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3308 sp->state[IDX_PAP] == STATE_OPENED) {
3309 mlen = sizeof(SUCCMSG) - 1;
3310 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3311 sizeof mlen, (const char *)&mlen,
3312 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3313 0);
3314 }
3315 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3316 sppp_cp_change_state(&pap, sp, STATE_OPENED);
3317 pap.tlu(sp);
3318 }
3319 break;
3320
3321 /* ack and nak are his authproto */
3322 case PAP_ACK:
3323 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3324 if (debug) {
3325 log(LOG_DEBUG, "%s%d: pap success",
3326 ifp->if_name, ifp->if_unit);
3327 name_len = *((char *)h);
3328 if (len > 5 && name_len) {
3329 addlog(": ");
3330 sppp_print_string((char*)(h+1), name_len);
3331 }
3332 addlog("\n");
3333 }
3334 x = splimp();
3335 sp->pp_flags &= ~PP_NEEDAUTH;
3336 if (sp->myauth.proto == PPP_PAP &&
3337 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3338 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3339 /*
3340 * We are authenticator for PAP but didn't
3341 * complete yet. Leave it to tlu to proceed
3342 * to network phase.
3343 */
3344 splx(x);
3345 break;
3346 }
3347 splx(x);
3348 sppp_phase_network(sp);
3349 break;
3350
3351 case PAP_NAK:
3352 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3353 if (debug) {
3354 log(LOG_INFO, "%s%d: pap failure",
3355 ifp->if_name, ifp->if_unit);
3356 name_len = *((char *)h);
3357 if (len > 5 && name_len) {
3358 addlog(": ");
3359 sppp_print_string((char*)(h+1), name_len);
3360 }
3361 addlog("\n");
3362 } else
3363 log(LOG_INFO, "%s%d: pap failure\n",
3364 ifp->if_name, ifp->if_unit);
3365 /* await LCP shutdown by authenticator */
3366 break;
3367
3368 default:
3369 /* Unknown PAP packet type -- ignore. */
3370 if (debug) {
3371 log(LOG_DEBUG, "%s%d: pap corrupted input "
3372 "<0x%x id=0x%x len=%d",
3373 ifp->if_name, ifp->if_unit,
3374 h->type, h->ident, ntohs(h->len));
3375 if (len > 4)
3376 sppp_print_bytes((u_char*)(h+1), len-4);
3377 addlog(">\n");
3378 }
3379 break;
3380
3381 }
3382}
3383
3384static void
3385sppp_pap_init(struct sppp *sp)
3386{
3387 /* PAP doesn't have STATE_INITIAL at all. */
3388 sp->state[IDX_PAP] = STATE_CLOSED;
3389 sp->fail_counter[IDX_PAP] = 0;
3390 callout_handle_init(&sp->ch[IDX_PAP]);
3391 callout_handle_init(&sp->pap_my_to_ch);
3392}
3393
3394static void
3395sppp_pap_open(struct sppp *sp)
3396{
3397 if (sp->hisauth.proto == PPP_PAP &&
3398 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3399 /* we are authenticator for PAP, start our timer */
3400 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3401 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3402 }
3403 if (sp->myauth.proto == PPP_PAP) {
3404 /* we are peer, send a request, and start a timer */
3405 pap.scr(sp);
3406 sp->pap_my_to_ch = timeout(sppp_pap_my_TO, (void *)sp,
3407 sp->lcp.timeout);
3408 }
3409}
3410
3411static void
3412sppp_pap_close(struct sppp *sp)
3413{
3414 if (sp->state[IDX_PAP] != STATE_CLOSED)
3415 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3416}
3417
3418/*
3419 * That's the timeout routine if we are authenticator. Since the
3420 * authenticator is basically passive in PAP, we can't do much here.
3421 */
3422static void
3423sppp_pap_TO(void *cookie)
3424{
3425 struct sppp *sp = (struct sppp *)cookie;
3426 STDDCL;
3427 int s;
3428
3429 s = splimp();
3430 if (debug)
3431 log(LOG_DEBUG, "%s%d: pap TO(%s) rst_counter = %d\n",
3432 ifp->if_name, ifp->if_unit,
3433 sppp_state_name(sp->state[IDX_PAP]),
3434 sp->rst_counter[IDX_PAP]);
3435
3436 if (--sp->rst_counter[IDX_PAP] < 0)
3437 /* TO- event */
3438 switch (sp->state[IDX_PAP]) {
3439 case STATE_REQ_SENT:
3440 pap.tld(sp);
3441 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3442 break;
3443 }
3444 else
3445 /* TO+ event, not very much we could do */
3446 switch (sp->state[IDX_PAP]) {
3447 case STATE_REQ_SENT:
3448 /* sppp_cp_change_state() will restart the timer */
3449 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3450 break;
3451 }
3452
3453 splx(s);
3454}
3455
3456/*
3457 * That's the timeout handler if we are peer. Since the peer is active,
3458 * we need to retransmit our PAP request since it is apparently lost.
3459 * XXX We should impose a max counter.
3460 */
3461static void
3462sppp_pap_my_TO(void *cookie)
3463{
3464 struct sppp *sp = (struct sppp *)cookie;
3465 STDDCL;
3466
3467 if (debug)
3468 log(LOG_DEBUG, "%s%d: pap peer TO\n",
3469 ifp->if_name, ifp->if_unit);
3470
3471 pap.scr(sp);
3472}
3473
3474static void
3475sppp_pap_tlu(struct sppp *sp)
3476{
3477 STDDCL;
3478 int x;
3479
3480 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3481
3482 if (debug)
3483 log(LOG_DEBUG, "%s%d: %s tlu\n",
3484 ifp->if_name, ifp->if_unit, pap.name);
3485
3486 x = splimp();
3487 /* indicate to LCP that we need to be closed down */
3488 sp->lcp.protos |= (1 << IDX_PAP);
3489
3490 if (sp->pp_flags & PP_NEEDAUTH) {
3491 /*
3492 * Remote is authenticator, but his auth proto didn't
3493 * complete yet. Defer the transition to network
3494 * phase.
3495 */
3496 splx(x);
3497 return;
3498 }
3499 splx(x);
3500 sppp_phase_network(sp);
3501}
3502
3503static void
3504sppp_pap_tld(struct sppp *sp)
3505{
3506 STDDCL;
3507
3508 if (debug)
3509 log(LOG_DEBUG, "%s%d: pap tld\n", ifp->if_name, ifp->if_unit);
3510 untimeout(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3511 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3512 sp->lcp.protos &= ~(1 << IDX_PAP);
3513
3514 lcp.Close(sp);
3515}
3516
3517static void
3518sppp_pap_scr(struct sppp *sp)
3519{
3520 STDDCL;
3521 u_char idlen, pwdlen;
3522
3523 sp->confid[IDX_PAP] = ++sp->pp_seq;
3524 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3525 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3526
3527 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3528 sizeof idlen, (const char *)&idlen,
3529 (unsigned)idlen, sp->myauth.name,
3530 sizeof pwdlen, (const char *)&pwdlen,
3531 (unsigned)pwdlen, sp->myauth.secret,
3532 0);
3533}
3534/*
3535 * Random miscellaneous functions.
3536 */
3537
3538/*
3539 * Send a PAP or CHAP proto packet.
3540 *
3541 * Varadic function, each of the elements for the ellipsis is of type
3542 * ``unsigned mlen, const u_char *msg''. Processing will stop iff
3543 * mlen == 0.
3544 */
3545
3546static void
3547sppp_auth_send(const struct cp *cp, struct sppp *sp, u_char type, u_char id,
3548 ...)
3549{
3550 STDDCL;
3551 struct ppp_header *h;
3552 struct lcp_header *lh;
3553 struct mbuf *m;
3554 u_char *p;
3555 int len;
3556 unsigned mlen;
3557 const char *msg;
3558 va_list ap;
3559
3560 MGETHDR (m, M_DONTWAIT, MT_DATA);
3561 if (! m)
3562 return;
3563 m->m_pkthdr.rcvif = 0;
3564
3565 h = mtod (m, struct ppp_header*);
3566 h->address = PPP_ALLSTATIONS; /* broadcast address */
3567 h->control = PPP_UI; /* Unnumbered Info */
3568 h->protocol = htons(cp->proto);
3569
3570 lh = (struct lcp_header*)(h + 1);
3571 lh->type = type;
3572 lh->ident = id;
3573 p = (u_char*) (lh+1);
3574
3575 va_start(ap, id);
3576 len = 0;
3577
3578 while ((mlen = va_arg(ap, unsigned)) != 0) {
3579 msg = va_arg(ap, const char *);
3580 len += mlen;
3581 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3582 va_end(ap);
3583 m_freem(m);
3584 return;
3585 }
3586
3587 bcopy(msg, p, mlen);
3588 p += mlen;
3589 }
3590 va_end(ap);
3591
3592 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3593 lh->len = htons (LCP_HEADER_LEN + len);
3594
3595 if (debug) {
3596 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
3597 ifp->if_name, ifp->if_unit, cp->name,
3598 sppp_auth_type_name(cp->proto, lh->type),
3599 lh->ident, ntohs(lh->len));
3600 if (len)
3601 sppp_print_bytes((u_char*) (lh+1), len);
3602 addlog(">\n");
3603 }
3604 if (IF_QFULL (&sp->pp_cpq)) {
3605 IF_DROP (&sp->pp_fastq);
3606 IF_DROP (&ifp->if_snd);
3607 m_freem (m);
3608 ++ifp->if_oerrors;
3609 } else
3610 IF_ENQUEUE (&sp->pp_cpq, m);
3611 if (! (ifp->if_flags & IFF_OACTIVE))
3612 (*ifp->if_start) (ifp);
3613 ifp->if_obytes += m->m_pkthdr.len + 3;
3614}
3615
3616/*
3617 * Flush interface queue.
3618 */
3619static void
3620sppp_qflush(struct ifqueue *ifq)
3621{
3622 struct mbuf *m, *n;
3623
3624 n = ifq->ifq_head;
3625 while ((m = n)) {
3626 n = m->m_act;
3627 m_freem (m);
3628 }
3629 ifq->ifq_head = 0;
3630 ifq->ifq_tail = 0;
3631 ifq->ifq_len = 0;
3632}
3633
3634/*
3635 * Send keepalive packets, every 10 seconds.
3636 */
3637static void
3638sppp_keepalive(void *dummy)
3639{
3640 struct sppp *sp;
3641 int s;
3642
3643 s = splimp();
3644 for (sp=spppq; sp; sp=sp->pp_next) {
3645 struct ifnet *ifp = &sp->pp_if;
3646
3647 /* Keepalive mode disabled or channel down? */
3648 if (! (sp->pp_flags & PP_KEEPALIVE) ||
3649 ! (ifp->if_flags & IFF_RUNNING))
3650 continue;
3651
3652 /* No keepalive in PPP mode if LCP not opened yet. */
3653 if (! (sp->pp_flags & PP_CISCO) &&
3654 sp->pp_phase < PHASE_AUTHENTICATE)
3655 continue;
3656
3657 if (sp->pp_alivecnt == MAXALIVECNT) {
3658 /* No keepalive packets got. Stop the interface. */
3659 printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
3660 if_down (ifp);
3661 sppp_qflush (&sp->pp_cpq);
3662 if (! (sp->pp_flags & PP_CISCO)) {
3663 /* XXX */
3664 /* Shut down the PPP link. */
3665 lcp.Down(sp);
3666 /* Initiate negotiation. XXX */
3667 lcp.Up(sp);
3668 }
3669 }
3670 if (sp->pp_alivecnt <= MAXALIVECNT)
3671 ++sp->pp_alivecnt;
3672 if (sp->pp_flags & PP_CISCO)
3673 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3674 sp->pp_rseq);
3675 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3676 long nmagic = htonl (sp->lcp.magic);
3677 sp->lcp.echoid = ++sp->pp_seq;
3678 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3679 sp->lcp.echoid, 4, &nmagic);
3680 }
3681 }
3682 splx(s);
3683 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
3684}
3685
3686/*
3687 * Get both IP addresses.
3688 */
3689static void
3690sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
3691{
3692 struct ifnet *ifp = &sp->pp_if;
3693 struct ifaddr *ifa;
3694 struct sockaddr_in *si, *sm;
3695 u_long ssrc, ddst;
3696
3697 ssrc = ddst = 0L;
3698 /*
3699 * Pick the first AF_INET address from the list,
3700 * aliases don't make any sense on a p2p link anyway.
3701 */
3702 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3703 ifa;
3704 ifa = ifa->ifa_link.tqe_next)
3705 if (ifa->ifa_addr->sa_family == AF_INET) {
3706 si = (struct sockaddr_in *)ifa->ifa_addr;
3707 sm = (struct sockaddr_in *)ifa->ifa_netmask;
3708 if (si)
3709 break;
3710 }
3711 if (ifa) {
3712 if (si && si->sin_addr.s_addr) {
3713 ssrc = si->sin_addr.s_addr;
3714 if (srcmask)
3715 *srcmask = ntohl(sm->sin_addr.s_addr);
3716 }
3717
3718 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3719 if (si && si->sin_addr.s_addr)
3720 ddst = si->sin_addr.s_addr;
3721 }
3722
3723 if (dst) *dst = ntohl(ddst);
3724 if (src) *src = ntohl(ssrc);
3725}
3726
3727/*
3728 * Set my IP address. Must be called at splimp.
3729 */
3730static void
3731sppp_set_ip_addr(struct sppp *sp, u_long src)
3732{
3733 struct ifnet *ifp = &sp->pp_if;
3734 struct ifaddr *ifa;
3735 struct sockaddr_in *si;
3736 u_long ssrc, ddst;
3737
3738 /*
3739 * Pick the first AF_INET address from the list,
3740 * aliases don't make any sense on a p2p link anyway.
3741 */
3742 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3743 ifa;
3744 ifa = ifa->ifa_link.tqe_next)
3745 if (ifa->ifa_addr->sa_family == AF_INET) {
3746 si = (struct sockaddr_in *)ifa->ifa_addr;
3747 if (si)
3748 break;
3749 }
3750 if (ifa && si)
3751 si->sin_addr.s_addr = htonl(src);
3752}
3753
3754static int
3755sppp_params(struct sppp *sp, int cmd, void *data)
3756{
3757 int subcmd;
3758 struct ifreq *ifr = (struct ifreq *)data;
3759 struct spppreq spr;
3760
3761 /*
3762 * ifr->ifr_data is supposed to point to a struct spppreq.
3763 * Check the cmd word first before attempting to fetch all the
3764 * data.
3765 */
3766 if ((subcmd = fuword(ifr->ifr_data)) == -1)
3767 return EFAULT;
3768
3769 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3770 return EFAULT;
3771
3772 switch (subcmd) {
3773 case SPPPIOGDEFS:
3774 if (cmd != SIOCGIFGENERIC)
3775 return EINVAL;
3776 /*
3777 * We copy over the entire current state, but clean
3778 * out some of the stuff we don't wanna pass up.
3779 * Remember, SIOCGIFGENERIC is unprotected, and can be
3780 * called by any user. No need to ever get PAP or
3781 * CHAP secrets back to userland anyway.
3782 */
3783 bcopy(sp, &spr.defs, sizeof(struct sppp));
3784 bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3785 bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3786 bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3787 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3788 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3789
3790 case SPPPIOSDEFS:
3791 if (cmd != SIOCSIFGENERIC)
3792 return EINVAL;
3793 /*
3794 * We have a very specific idea of which fields we allow
3795 * being passed back from userland, so to not clobber our
3796 * current state. For one, we only allow setting
3797 * anything if LCP is in dead phase. Once the LCP
3798 * negotiations started, the authentication settings must
3799 * not be changed again. (The administrator can force an
3800 * ifconfig down in order to get LCP back into dead
3801 * phase.)
3802 *
3803 * Also, we only allow for authentication parameters to be
3804 * specified.
3805 *
3806 * XXX Should allow to set or clear pp_flags.
3807 *
3808 * Finally, if the respective authentication protocol to
3809 * be used is set differently than 0, but the secret is
3810 * passed as all zeros, we don't trash the existing secret.
3811 * This allows an administrator to change the system name
3812 * only without clobbering the secret (which he didn't get
3813 * back in a previous SPPPIOGDEFS call). However, the
3814 * secrets are cleared if the authentication protocol is
3815 * reset to 0.
3816 */
3817 if (sp->pp_phase != PHASE_DEAD)
3818 return EBUSY;
3819
3820 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
3821 spr.defs.myauth.proto != PPP_CHAP) ||
3822 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
3823 spr.defs.hisauth.proto != PPP_CHAP))
3824 return EINVAL;
3825
3826 if (spr.defs.myauth.proto == 0)
3827 /* resetting myauth */
3828 bzero(&sp->myauth, sizeof sp->myauth);
3829 else {
3830 /* setting/changing myauth */
3831 sp->myauth.proto = spr.defs.myauth.proto;
3832 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
3833 if (spr.defs.myauth.secret[0] != '\0')
3834 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
3835 AUTHKEYLEN);
3836 }
3837 if (spr.defs.hisauth.proto == 0)
3838 /* resetting hisauth */
3839 bzero(&sp->hisauth, sizeof sp->hisauth);
3840 else {
3841 /* setting/changing hisauth */
3842 sp->hisauth.proto = spr.defs.hisauth.proto;
3843 sp->hisauth.flags = spr.defs.hisauth.flags;
3844 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
3845 if (spr.defs.hisauth.secret[0] != '\0')
3846 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
3847 AUTHKEYLEN);
3848 }
3849 break;
3850
3851 default:
3852 return EINVAL;
3853 }
3854
3855 return 0;
3856}
3857
3858static void
3859sppp_phase_network(struct sppp *sp)
3860{
3861 struct ifnet *ifp = &sp->pp_if;
3862 int i;
3863 u_long mask;
3864
3865 sp->pp_phase = PHASE_NETWORK;
3866
3867 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
3868 sppp_phase_name(sp->pp_phase));
3869
3870 /* Notify NCPs now. */
3871 for (i = 0; i < IDX_COUNT; i++)
3872 if ((cps[i])->flags & CP_NCP)
3873 (cps[i])->Open(sp);
3874
3875 /* Send Up events to all NCPs. */
3876 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
3877 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
3878 (cps[i])->Up(sp);
3879
3880 /* if no NCP is starting, all this was in vain, close down */
3881 sppp_lcp_check_and_close(sp);
3882}
3883
3884
3885static const char *
3886sppp_cp_type_name(u_char type)
3887{
3888 static char buf[12];
3889 switch (type) {
3890 case CONF_REQ: return "conf-req";
3891 case CONF_ACK: return "conf-ack";
3892 case CONF_NAK: return "conf-nak";
3893 case CONF_REJ: return "conf-rej";
3894 case TERM_REQ: return "term-req";
3895 case TERM_ACK: return "term-ack";
3896 case CODE_REJ: return "code-rej";
3897 case PROTO_REJ: return "proto-rej";
3898 case ECHO_REQ: return "echo-req";
3899 case ECHO_REPLY: return "echo-reply";
3900 case DISC_REQ: return "discard-req";
3901 }
3902 sprintf (buf, "0x%x", type);
3903 return buf;
3904}
3905
3906static const char *
3907sppp_auth_type_name(u_short proto, u_char type)
3908{
3909 static char buf[12];
3910 switch (proto) {
3911 case PPP_CHAP:
3912 switch (type) {
3913 case CHAP_CHALLENGE: return "challenge";
3914 case CHAP_RESPONSE: return "response";
3915 case CHAP_SUCCESS: return "success";
3916 case CHAP_FAILURE: return "failure";
3917 }
3918 case PPP_PAP:
3919 switch (type) {
3920 case PAP_REQ: return "req";
3921 case PAP_ACK: return "ack";
3922 case PAP_NAK: return "nak";
3923 }
3924 }
3925 sprintf (buf, "0x%x", type);
3926 return buf;
3927}
3928
3929static const char *
3930sppp_lcp_opt_name(u_char opt)
3931{
3932 static char buf[12];
3933 switch (opt) {
3934 case LCP_OPT_MRU: return "mru";
3935 case LCP_OPT_ASYNC_MAP: return "async-map";
3936 case LCP_OPT_AUTH_PROTO: return "auth-proto";
3937 case LCP_OPT_QUAL_PROTO: return "qual-proto";
3938 case LCP_OPT_MAGIC: return "magic";
3939 case LCP_OPT_PROTO_COMP: return "proto-comp";
3940 case LCP_OPT_ADDR_COMP: return "addr-comp";
3941 }
3942 sprintf (buf, "0x%x", opt);
3943 return buf;
3944}
3945
3946static const char *
3947sppp_ipcp_opt_name(u_char opt)
3948{
3949 static char buf[12];
3950 switch (opt) {
3951 case IPCP_OPT_ADDRESSES: return "addresses";
3952 case IPCP_OPT_COMPRESSION: return "compression";
3953 case IPCP_OPT_ADDRESS: return "address";
3954 }
3955 sprintf (buf, "0x%x", opt);
3956 return buf;
3957}
3958
3959static const char *
3960sppp_state_name(int state)
3961{
3962 switch (state) {
3963 case STATE_INITIAL: return "initial";
3964 case STATE_STARTING: return "starting";
3965 case STATE_CLOSED: return "closed";
3966 case STATE_STOPPED: return "stopped";
3967 case STATE_CLOSING: return "closing";
3968 case STATE_STOPPING: return "stopping";
3969 case STATE_REQ_SENT: return "req-sent";
3970 case STATE_ACK_RCVD: return "ack-rcvd";
3971 case STATE_ACK_SENT: return "ack-sent";
3972 case STATE_OPENED: return "opened";
3973 }
3974 return "illegal";
3975}
3976
3977static const char *
3978sppp_phase_name(enum ppp_phase phase)
3979{
3980 switch (phase) {
3981 case PHASE_DEAD: return "dead";
3982 case PHASE_ESTABLISH: return "establish";
3983 case PHASE_TERMINATE: return "terminate";
3984 case PHASE_AUTHENTICATE: return "authenticate";
3985 case PHASE_NETWORK: return "network";
3986 }
3987 return "illegal";
3988}
3989
3990static const char *
3991sppp_proto_name(u_short proto)
3992{
3993 static char buf[12];
3994 switch (proto) {
3995 case PPP_LCP: return "lcp";
3996 case PPP_IPCP: return "ipcp";
3997 case PPP_PAP: return "pap";
3998 case PPP_CHAP: return "chap";
3999 }
4000 sprintf(buf, "0x%x", (unsigned)proto);
4001 return buf;
4002}
4003
4004static void
4005sppp_print_bytes(const u_char *p, u_short len)
4006{
4007 addlog(" %x", *p++);
4008 while (--len > 0)
4009 addlog("-%x", *p++);
4010}
4011
4012static void
4013sppp_print_string(const char *p, u_short len)
4014{
4015 u_char c;
4016
4017 while (len-- > 0) {
4018 c = *p++;
4019 /*
4020 * Print only ASCII chars directly. RFC 1994 recommends
4021 * using only them, but we don't rely on it. */
4022 if (c < ' ' || c > '~')
4023 addlog("\\x%x", c);
4024 else
4025 addlog("%c", c);
4026 }
4027}
4028
4029static const char *
4030sppp_dotted_quad(u_long addr)
4031{
4032 static char s[16];
4033 sprintf(s, "%d.%d.%d.%d",
4034 (addr >> 24) & 0xff,
4035 (addr >> 16) & 0xff,
4036 (addr >> 8) & 0xff,
4037 addr & 0xff);
4038 return s;
4039}
4040
4041static int
4042sppp_strnlen(u_char *p, int max)
4043{
4044 int len;
4045
4046 for (len = 0; len < max && *p; ++p)
4047 ++len;
4048 return len;
4049}
4050
4051/* a dummy, used to drop uninteresting events */
4052static void
4053sppp_null(struct sppp *unused)
4054{
4055 /* do just nothing */
4056}
4057/*
4058 * This file is large. Tell emacs to highlight it nevertheless.
4059 *
4060 * Local Variables:
4061 * hilit-auto-highlight-maxout: 120000
4062 * End:
4063 */