1 //==========================================================================
5 //==========================================================================
6 //####ECOSGPLCOPYRIGHTBEGIN####
7 // -------------------------------------------
8 // This file is part of eCos, the Embedded Configurable Operating System.
9 // Portions created by Nick Garnett are
10 // Copyright (C) 2003 eCosCentric Ltd.
12 // eCos is free software; you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 2 or (at your option) any later version.
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // You should have received a copy of the GNU General Public License along
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 // As a special exception, if other files instantiate templates or use macros
26 // or inline functions from this file, or you compile this file and link it
27 // with other works to produce a work based on this file, this file does not
28 // by itself cause the resulting work to be covered by the GNU General Public
29 // License. However the source code for this file must still be made available
30 // in accordance with section (3) of the GNU General Public License.
32 // This exception does not invalidate any other reasons why a work based on
33 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //####BSDCOPYRIGHTBEGIN####
39 // -------------------------------------------
41 // Portions of this software may have been derived from OpenBSD,
42 // FreeBSD or other sources, and are covered by the appropriate
43 // copyright disclaimers included herein.
45 // -------------------------------------------
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
51 * ipcp.c - PPP IP Control Protocol.
53 * Copyright (c) 1989 Carnegie Mellon University.
54 * All rights reserved.
56 * Redistribution and use in source and binary forms are permitted
57 * provided that the above copyright notice and this paragraph are
58 * duplicated in all such forms and that any documentation,
59 * advertising materials, and other materials related to such
60 * distribution and use acknowledge that the software was developed
61 * by Carnegie Mellon University. The name of the
62 * University may not be used to endorse or promote products derived
63 * from this software without specific prior written permission.
64 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
65 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
66 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
70 //static char rcsid[] = "$FreeBSD: src/usr.sbin/pppd/ipcp.c,v 1.12 1999/08/28 01:19:03 peter Exp $";
79 #include <cyg/ppp/syslog.h>
81 #include <sys/param.h>
82 #include <sys/types.h>
83 #include <sys/socket.h>
84 #include <netinet/in.h>
86 #include <pkgconf/system.h>
87 #include <pkgconf/ppp.h>
88 #include "cyg/ppp/pppd.h"
89 #include "cyg/ppp/fsm.h"
90 #include "cyg/ppp/ipcp.h"
91 #include "cyg/ppp/ppp_io.h"
94 #include <cyg/ns/dns/dns.h>
97 #define option_error(msg) db_printf("Option error: %s\n", msg )
100 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
101 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
102 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
103 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
106 static int cis_received[NUM_PPP]; /* # Conf-Reqs received */
107 static int default_route_set[NUM_PPP]; /* Have set up a default route */
108 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
111 * Callbacks for fsm code. (CI = Configuration Information)
113 static void ipcp_resetci __P((fsm *)); /* Reset our CI */
114 static int ipcp_cilen __P((fsm *)); /* Return length of our CI */
115 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
116 static int ipcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
117 static int ipcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
118 static int ipcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
119 static int ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
120 static void ipcp_up __P((fsm *)); /* We're UP */
121 static void ipcp_down __P((fsm *)); /* We're DOWN */
122 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
124 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
126 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
127 ipcp_resetci, /* Reset our Configuration Information */
128 ipcp_cilen, /* Length of our Configuration Information */
129 ipcp_addci, /* Add our Configuration Information */
130 ipcp_ackci, /* ACK our Configuration Information */
131 ipcp_nakci, /* NAK our Configuration Information */
132 ipcp_rejci, /* Reject our Configuration Information */
133 ipcp_reqci, /* Request peer's Configuration Information */
134 ipcp_up, /* Called when fsm reaches OPENED state */
135 ipcp_down, /* Called when fsm leaves OPENED state */
136 NULL, /* Called when we want the lower layer up */
137 ipcp_finished, /* Called when we want the lower layer down */
138 NULL, /* Called when Protocol-Reject received */
139 NULL, /* Retransmission is necessary */
140 NULL, /* Called to handle protocol-specific codes */
141 "IPCP" /* String name of protocol */
145 * Protocol entry points from main code.
147 static void ipcp_init __P((int));
148 static void ipcp_open __P((int));
149 static void ipcp_close __P((int, char *));
150 static void ipcp_lowerup __P((int));
151 static void ipcp_lowerdown __P((int));
152 static void ipcp_input __P((int, u_char *, int));
153 static void ipcp_protrej __P((int));
154 static int ipcp_printpkt __P((u_char *, int,
155 void (*) __P((void *, char *, ...)), void *));
156 static void ip_check_options __P((void));
157 static int ip_demand_conf __P((int));
158 static int ip_active_pkt __P((u_char *, int));
160 struct protent ipcp_protent = {
178 static void ipcp_clear_addrs __P((int));
181 * Lengths of configuration options.
184 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
185 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
186 #define CILEN_ADDR 6 /* new-style single address option */
187 #define CILEN_ADDRS 10 /* old-style dual address option */
190 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
191 (x) == CONFNAK ? "NAK" : "REJ")
195 * Make a string representation of a network IP address.
203 ipaddr = ntohl(ipaddr);
205 sprintf(b, "%d.%d.%d.%d",
206 (u_char)(ipaddr >> 24),
207 (u_char)(ipaddr >> 16),
208 (u_char)(ipaddr >> 8),
215 * ipcp_init - Initialize IPCP.
221 fsm *f = &ipcp_fsm[unit];
222 ipcp_options *wo = &ipcp_wantoptions[unit];
223 ipcp_options *ao = &ipcp_allowoptions[unit];
226 f->protocol = PPP_IPCP;
227 f->callbacks = &ipcp_callbacks;
228 fsm_init(&ipcp_fsm[unit]);
230 memset(wo, 0, sizeof(*wo));
231 memset(ao, 0, sizeof(*ao));
235 wo->vj_protocol = IPCP_VJ_COMP;
236 wo->maxslotindex = MAX_STATES - 1; /* really max index */
238 wo->ouraddr = ppp_tty.options->our_address;
239 wo->hisaddr = ppp_tty.options->his_address;
240 wo->default_route = ppp_tty.options->default_route;
242 #ifdef CYGOPT_PPP_NS_NEGOTIATE
246 /* max slots and slot-id compression are currently hardwired in */
247 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
252 ao->maxslotindex = MAX_STATES - 1;
256 * XXX These control whether the user may use the proxyarp
257 * and defaultroute options.
260 ao->default_route = 1;
265 * ipcp_open - IPCP is allowed to come up.
271 fsm_open(&ipcp_fsm[unit]);
276 * ipcp_close - Take IPCP down.
279 ipcp_close(unit, reason)
283 fsm_close(&ipcp_fsm[unit], reason);
288 * ipcp_lowerup - The lower layer is up.
294 fsm_lowerup(&ipcp_fsm[unit]);
299 * ipcp_lowerdown - The lower layer is down.
305 fsm_lowerdown(&ipcp_fsm[unit]);
310 * ipcp_input - Input IPCP packet.
313 ipcp_input(unit, p, len)
318 fsm_input(&ipcp_fsm[unit], p, len);
323 * ipcp_protrej - A Protocol-Reject was received for IPCP.
325 * Pretend the lower layer went down, so we shut up.
331 fsm_lowerdown(&ipcp_fsm[unit]);
336 * ipcp_resetci - Reset our CI.
342 ipcp_options *wo = &ipcp_wantoptions[f->unit];
344 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
345 if (wo->ouraddr == 0)
346 wo->accept_local = 1;
347 if (wo->hisaddr == 0)
348 wo->accept_remote = 1;
349 ipcp_gotoptions[f->unit] = *wo;
350 cis_received[f->unit] = 0;
355 * ipcp_cilen - Return length of our CI.
361 ipcp_options *go = &ipcp_gotoptions[f->unit];
362 ipcp_options *wo = &ipcp_wantoptions[f->unit];
363 ipcp_options *ho = &ipcp_hisoptions[f->unit];
366 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
367 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
370 * First see if we want to change our options to the old
371 * forms because we have received old forms from the peer.
373 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
374 /* use the old style of address negotiation */
378 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
379 /* try an older style of VJ negotiation */
380 if (cis_received[f->unit] == 0) {
381 #ifndef PPP_INHIBIT_OLD_VJ_COMPRESSION
382 /* keep trying the new style until we see some CI from the peer */
386 /* use the old style only if the peer did */
387 if (ho->neg_vj && ho->old_vj) {
390 go->vj_protocol = ho->vj_protocol;
395 len = LENCIADDR(go->neg_addr, go->old_addrs) +
396 LENCIVJ(go->neg_vj, go->old_vj);
398 #ifdef CYGOPT_PPP_NS_NEGOTIATE
399 len += LENCIADDR(go->neg_dns1, 0) +
400 LENCIADDR(go->neg_wins1, 0) +
401 LENCIADDR(go->neg_dns2, 0) +
402 LENCIADDR(go->neg_wins2, 0);
409 * ipcp_addci - Add our desired CIs to a packet.
412 ipcp_addci(f, ucp, lenp)
417 ipcp_options *go = &ipcp_gotoptions[f->unit];
420 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
422 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
423 if (len >= vjlen) { \
425 PUTCHAR(vjlen, ucp); \
426 PUTSHORT(val, ucp); \
428 PUTCHAR(maxslotindex, ucp); \
429 PUTCHAR(cflag, ucp); \
436 #define ADDCIADDR(opt, neg, old, val1, val2) \
438 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
439 if (len >= addrlen) { \
442 PUTCHAR(addrlen, ucp); \
454 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
455 go->old_addrs, go->ouraddr, go->hisaddr);
457 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
458 go->maxslotindex, go->cflag);
460 #ifdef CYGOPT_PPP_NS_NEGOTIATE
461 ADDCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
462 ADDCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
463 ADDCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
464 ADDCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
472 * ipcp_ackci - Ack our CIs.
479 ipcp_ackci(f, p, len)
484 ipcp_options *go = &ipcp_gotoptions[f->unit];
485 u_short cilen, citype, cishort;
487 u_char cimaxslotindex, cicflag;
490 * CIs must be in exactly the same order that we sent...
491 * Check packet length and CI length at each step.
492 * If we find any deviations, then this packet is bad.
495 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
497 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
498 if ((len -= vjlen) < 0) \
500 GETCHAR(citype, p); \
502 if (cilen != vjlen || \
505 GETSHORT(cishort, p); \
506 if (cishort != val) \
509 GETCHAR(cimaxslotindex, p); \
510 if (cimaxslotindex != maxslotindex) \
512 GETCHAR(cicflag, p); \
513 if (cicflag != cflag) \
518 #define ACKCIADDR(opt, neg, old, val1, val2) \
520 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
522 if ((len -= addrlen) < 0) \
524 GETCHAR(citype, p); \
526 if (cilen != addrlen || \
531 if (val1 != cilong) \
536 if (val2 != cilong) \
541 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
542 go->old_addrs, go->ouraddr, go->hisaddr);
544 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
545 go->maxslotindex, go->cflag);
547 #ifdef CYGOPT_PPP_NS_NEGOTIATE
548 ACKCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
549 ACKCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
550 ACKCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
551 ACKCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
555 * If there are any remaining CIs, then this packet is bad.
562 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
567 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
568 * This should not modify any state if the Nak is bad
569 * or if IPCP is in the OPENED state.
576 ipcp_nakci(f, p, len)
581 ipcp_options *go = &ipcp_gotoptions[f->unit];
582 u_char cimaxslotindex, cicflag;
583 u_char citype, cilen, *next;
585 u_int32_t ciaddr1, ciaddr2, l;
586 ipcp_options no; /* options we've seen Naks for */
587 ipcp_options try; /* options to request next time */
589 BZERO(&no, sizeof(no));
593 * Any Nak'd CIs must be in exactly the same order that we sent.
594 * Check packet length and CI length at each step.
595 * If we find any deviations, then this packet is bad.
597 #define NAKCIADDR(opt, neg, old, code) \
599 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
605 ciaddr1 = htonl(l); \
608 ciaddr2 = htonl(l); \
616 #define NAKCIVJ(opt, neg, code) \
618 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
623 GETSHORT(cishort, p); \
629 * Accept the peer's idea of {our,his} address, if different
630 * from our idea, only if the accept_{local,remote} flag is set.
632 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
633 if (go->accept_local && ciaddr1) { /* Do we know our address? */
634 try.ouraddr = ciaddr1;
635 IPCPDEBUG((LOG_INFO, "local IP address %s",
638 if (go->accept_remote && ciaddr2) { /* Does he know his? */
639 try.hisaddr = ciaddr2;
640 IPCPDEBUG((LOG_INFO, "remote IP address %s",
646 * Accept the peer's value of maxslotindex provided that it
647 * is less than what we asked for. Turn off slot-ID compression
648 * if the peer wants. Send old-style compress-type option if
651 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
652 if (cilen == CILEN_VJ) {
653 GETCHAR(cimaxslotindex, p);
655 if (cishort == IPCP_VJ_COMP) {
657 if (cimaxslotindex < go->maxslotindex)
658 try.maxslotindex = cimaxslotindex;
665 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
667 try.vj_protocol = cishort;
674 #ifdef CYGOPT_PPP_NS_NEGOTIATE
676 * Accept the peer's idea of the DNS and WINS addresses
678 NAKCIADDR(CI_MS_DNS1, neg_dns1, 0, try.dnsaddr[0] = ciaddr1;);
679 NAKCIADDR(CI_MS_WINS1, neg_wins1, 0, try.winsaddr[0] = ciaddr1;);
680 NAKCIADDR(CI_MS_DNS2, neg_dns2, 0, try.dnsaddr[1] = ciaddr1;);
681 NAKCIADDR(CI_MS_WINS2, neg_wins2, 0, try.winsaddr[1] = ciaddr1;);
682 #ifdef CYGOPT_PPP_DNS_CONFIGURE
683 if (try.dnsaddr[0] != 0 )
685 cyg_dns_res_init((struct in_addr *)&try.dnsaddr[0]);
691 * There may be remaining CIs, if the peer is requesting negotiation
692 * on an option that we didn't include in our request packet.
693 * If they want to negotiate about IP addresses, we comply.
694 * If they want us to ask for compression, we refuse.
696 while (len > CILEN_VOID) {
699 if( (len -= cilen) < 0 )
701 next = p + cilen - 2;
704 case CI_COMPRESSTYPE:
705 if (go->neg_vj || no.neg_vj ||
706 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
711 if ((go->neg_addr && go->old_addrs) || no.old_addrs
712 || cilen != CILEN_ADDRS)
718 if (ciaddr1 && go->accept_local)
719 try.ouraddr = ciaddr1;
722 if (ciaddr2 && go->accept_remote)
723 try.hisaddr = ciaddr2;
727 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
732 if (ciaddr1 && go->accept_local)
733 try.ouraddr = ciaddr1;
734 if (try.ouraddr != 0)
742 /* If there is still anything left, this packet is bad. */
747 * OK, the Nak is good. Now we can update state.
749 if (f->state != OPENED)
755 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
761 * ipcp_rejci - Reject some of our CIs.
764 ipcp_rejci(f, p, len)
769 ipcp_options *go = &ipcp_gotoptions[f->unit];
770 u_char cimaxslotindex, ciflag, cilen;
773 ipcp_options try; /* options to request next time */
774 #ifdef CYGOPT_PPP_NS_NEGOTIATE
775 u_char citype, *next;
780 * Any Rejected CIs must be in exactly the same order that we sent.
781 * Check packet length and CI length at each step.
782 * If we find any deviations, then this packet is bad.
784 #define REJCIADDR(opt, neg, old, val1, val2) \
786 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
794 /* Check rejected value. */ \
795 if (cilong != val1) \
800 /* Check rejected value. */ \
801 if (cilong != val2) \
807 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
809 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
814 GETSHORT(cishort, p); \
815 /* Check rejected value. */ \
816 if (cishort != val) \
819 GETCHAR(cimaxslotindex, p); \
820 if (cimaxslotindex != maxslot) \
822 GETCHAR(ciflag, p); \
823 if (ciflag != cflag) \
829 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
830 go->old_addrs, go->ouraddr, go->hisaddr);
832 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
833 go->maxslotindex, go->cflag);
835 #ifdef CYGOPT_PPP_NS_NEGOTIATE
837 * There may be remaining CIs, if the peer is unable to support
838 * DNS or WINS negotiation. If so, just turn them off
842 while (len > CILEN_VOID) {
845 if( (len -= cilen) < 0 )
847 next = p + cilen - 2;
868 * If there are any remaining CIs, then this packet is bad.
873 * Now we can update state.
875 if (f->state != OPENED)
880 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
886 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
888 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
889 * appropriately. If reject_if_disagree is non-zero, doesn't return
890 * CONFNAK; returns CONFREJ if it can't return CONFACK.
893 ipcp_reqci(f, inp, len, reject_if_disagree)
895 u_char *inp; /* Requested CIs */
896 int *len; /* Length of requested CIs */
897 int reject_if_disagree;
899 ipcp_options *wo = &ipcp_wantoptions[f->unit];
900 ipcp_options *ho = &ipcp_hisoptions[f->unit];
901 ipcp_options *ao = &ipcp_allowoptions[f->unit];
902 ipcp_options *go = &ipcp_gotoptions[f->unit];
903 u_char *cip, *next; /* Pointer to current and next CIs */
904 u_short cilen, citype; /* Parsed len, type */
905 u_short cishort; /* Parsed short value */
906 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
907 int rc = CONFACK; /* Final packet return code */
908 int orc; /* Individual option return code */
909 u_char *p; /* Pointer to next char to parse */
910 u_char *ucp = inp; /* Pointer to current output char */
911 int l = *len; /* Length left */
912 u_char maxslotindex, cflag;
915 cis_received[f->unit] = 1;
918 * Reset all his options.
920 BZERO(ho, sizeof(*ho));
923 * Process all his options.
927 orc = CONFACK; /* Assume success */
928 cip = p = next; /* Remember begining of CI */
929 if (l < 2 || /* Not enough data for CI header or */
930 p[1] < 2 || /* CI length too small or */
931 p[1] > l) { /* CI length too big? */
932 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
933 orc = CONFREJ; /* Reject bad CI */
934 cilen = l; /* Reject till end of packet */
935 l = 0; /* Don't loop again */
938 GETCHAR(citype, p); /* Parse CI type */
939 GETCHAR(cilen, p); /* Parse CI length */
940 l -= cilen; /* Adjust remaining length */
941 next += cilen; /* Step to next CI */
943 switch (citype) { /* Check CI type */
945 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
947 cilen != CILEN_ADDRS) { /* Check CI length */
948 orc = CONFREJ; /* Reject CI */
953 * If he has no address, or if we both have his address but
954 * disagree about it, then NAK it with our idea.
955 * In particular, if we don't know his address, but he does,
958 GETLONG(tl, p); /* Parse source address (his) */
960 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
961 if (ciaddr1 != wo->hisaddr
962 && (ciaddr1 == 0 || !wo->accept_remote)) {
964 if (!reject_if_disagree) {
965 DECPTR(sizeof(u_int32_t), p);
966 tl = ntohl(wo->hisaddr);
969 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
971 * If neither we nor he knows his address, reject the option.
974 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
979 * If he doesn't know our address, or if we both have our address
980 * but disagree about it, then NAK it with our idea.
982 GETLONG(tl, p); /* Parse desination address (ours) */
984 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
985 if (ciaddr2 != wo->ouraddr) {
986 if (ciaddr2 == 0 || !wo->accept_local) {
988 if (!reject_if_disagree) {
989 DECPTR(sizeof(u_int32_t), p);
990 tl = ntohl(wo->ouraddr);
994 go->ouraddr = ciaddr2; /* accept peer's idea */
1000 ho->hisaddr = ciaddr1;
1001 ho->ouraddr = ciaddr2;
1005 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
1007 if (!ao->neg_addr ||
1008 cilen != CILEN_ADDR) { /* Check CI length */
1009 orc = CONFREJ; /* Reject CI */
1014 * If he has no address, or if we both have his address but
1015 * disagree about it, then NAK it with our idea.
1016 * In particular, if we don't know his address, but he does,
1019 GETLONG(tl, p); /* Parse source address (his) */
1020 ciaddr1 = htonl(tl);
1021 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
1022 if (ciaddr1 != wo->hisaddr
1023 && (ciaddr1 == 0 || !wo->accept_remote)) {
1025 if (!reject_if_disagree) {
1026 DECPTR(sizeof(u_int32_t), p);
1027 tl = ntohl(wo->hisaddr);
1030 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1032 * Don't ACK an address of 0.0.0.0 - reject it instead.
1035 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1040 ho->hisaddr = ciaddr1;
1045 /* Microsoft primary or secondary DNS request */
1046 d = citype == CI_MS_DNS2;
1047 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
1049 /* If we do not have a DNS address then we cannot send it */
1050 if (ao->dnsaddr[d] == 0 ||
1051 cilen != CILEN_ADDR) { /* Check CI length */
1052 orc = CONFREJ; /* Reject CI */
1056 if (htonl(tl) != ao->dnsaddr[d]) {
1057 DECPTR(sizeof(u_int32_t), p);
1058 tl = ntohl(ao->dnsaddr[d]);
1066 /* Microsoft primary or secondary WINS request */
1067 d = citype == CI_MS_WINS2;
1068 IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
1070 /* If we do not have a DNS address then we cannot send it */
1071 if (ao->winsaddr[d] == 0 ||
1072 cilen != CILEN_ADDR) { /* Check CI length */
1073 orc = CONFREJ; /* Reject CI */
1077 if (htonl(tl) != ao->winsaddr[d]) {
1078 DECPTR(sizeof(u_int32_t), p);
1079 tl = ntohl(ao->winsaddr[d]);
1085 case CI_COMPRESSTYPE:
1086 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
1088 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1092 GETSHORT(cishort, p);
1093 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
1095 if (!(cishort == IPCP_VJ_COMP ||
1096 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1102 ho->vj_protocol = cishort;
1103 if (cilen == CILEN_VJ) {
1104 GETCHAR(maxslotindex, p);
1105 if (maxslotindex > ao->maxslotindex) {
1107 if (!reject_if_disagree){
1109 PUTCHAR(ao->maxslotindex, p);
1113 if (cflag && !ao->cflag) {
1115 if (!reject_if_disagree){
1117 PUTCHAR(wo->cflag, p);
1120 ho->maxslotindex = maxslotindex;
1124 ho->maxslotindex = MAX_STATES - 1;
1135 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
1137 if (orc == CONFACK && /* Good CI */
1138 rc != CONFACK) /* but prior CI wasnt? */
1139 continue; /* Don't send this one */
1141 if (orc == CONFNAK) { /* Nak this CI? */
1142 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1143 orc = CONFREJ; /* Get tough if so */
1145 if (rc == CONFREJ) /* Rejecting prior CI? */
1146 continue; /* Don't send this one */
1147 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1148 rc = CONFNAK; /* Not anymore... */
1149 ucp = inp; /* Backup */
1154 if (orc == CONFREJ && /* Reject this CI */
1155 rc != CONFREJ) { /* but no prior ones? */
1157 ucp = inp; /* Backup */
1160 /* Need to move CI? */
1162 BCOPY(cip, ucp, cilen); /* Move it */
1164 /* Update output pointer */
1169 * If we aren't rejecting this packet, and we want to negotiate
1170 * their address, and they didn't send their address, then we
1171 * send a NAK with a CI_ADDR option appended. We assume the
1172 * input buffer is long enough that we can append the extra
1175 if (rc != CONFREJ && !ho->neg_addr &&
1176 wo->req_addr && !reject_if_disagree) {
1177 if (rc == CONFACK) {
1179 ucp = inp; /* reset pointer */
1180 wo->req_addr = 0; /* don't ask again */
1182 PUTCHAR(CI_ADDR, ucp);
1183 PUTCHAR(CILEN_ADDR, ucp);
1184 tl = ntohl(wo->hisaddr);
1188 *len = ucp - inp; /* Compute output length */
1189 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1190 return (rc); /* Return final code */
1195 * ip_check_options - check that any IP-related options are OK,
1196 * and assign appropriate defaults.
1203 ipcp_options *wo = &ipcp_wantoptions[0];
1206 * Default our local IP address based on our hostname.
1207 * If local IP address already given, don't bother.
1209 if (wo->ouraddr == 0 && !disable_defaultip) {
1211 * Look up our hostname (possibly with domain name appended)
1212 * and take the first IP address as our local IP address.
1213 * If there isn't an IP address for our hostname, too bad.
1215 wo->accept_local = 1; /* don't insist on this default value */
1216 if ((hp = gethostbyname(cyg_ppp_hostname)) != NULL) {
1217 local = *(u_int32_t *)hp->h_addr;
1218 if (local != 0 && !bad_ip_adrs(local))
1219 wo->ouraddr = local;
1223 if (demand && wo->hisaddr == 0) {
1224 option_error("remote IP address required for demand-dialling\n");
1230 * ip_demand_conf - configure the interface as though
1231 * IPCP were up, for use with dial-on-demand.
1237 ipcp_options *wo = &ipcp_wantoptions[u];
1239 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1243 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1245 if (wo->default_route)
1246 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1247 default_route_set[u] = 1;
1249 if (sifproxyarp(u, wo->hisaddr))
1250 proxy_arp_set[u] = 1;
1252 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1253 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1260 * ipcp_up - IPCP has come UP.
1262 * Configure the IP network interface appropriately and bring it up.
1269 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1270 ipcp_options *go = &ipcp_gotoptions[f->unit];
1271 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1273 np_up(f->unit, PPP_IP);
1274 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1277 * We must have a non-zero IP address for both ends of the link.
1280 ho->hisaddr = wo->hisaddr;
1282 if (ho->hisaddr == 0) {
1283 syslog(LOG_ERR, "Could not determine remote IP address");
1284 ipcp_close(f->unit, "Could not determine remote IP address");
1287 if (go->ouraddr == 0) {
1288 syslog(LOG_ERR, "Could not determine local IP address");
1289 ipcp_close(f->unit, "Could not determine local IP address");
1294 * Check that the peer is allowed to use the IP address it wants.
1296 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1297 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1298 ip_ntoa(ho->hisaddr));
1299 ipcp_close(f->unit, "Unauthorized remote IP address");
1303 /* set tcp compression */
1304 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1307 * If we are doing dial-on-demand, the interface is already
1308 * configured, so we put out any saved-up packets, then set the
1309 * interface to pass IP packets.
1312 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1313 if (go->ouraddr != wo->ouraddr)
1314 syslog(LOG_WARNING, "Local IP address changed to %s",
1315 ip_ntoa(go->ouraddr));
1316 if (ho->hisaddr != wo->hisaddr)
1317 syslog(LOG_WARNING, "Remote IP address changed to %s",
1318 ip_ntoa(ho->hisaddr));
1319 ipcp_clear_addrs(f->unit);
1321 /* Set the interface to the new addresses */
1322 mask = GetMask(go->ouraddr);
1323 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1324 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1325 ipcp_close(f->unit, "Interface configuration failed");
1329 /* assign a default route through the interface if required */
1330 if (ipcp_wantoptions[f->unit].default_route)
1331 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1332 default_route_set[f->unit] = 1;
1334 /* Make a proxy ARP entry if requested. */
1335 if (ipcp_wantoptions[f->unit].proxy_arp)
1336 if (sifproxyarp(f->unit, ho->hisaddr))
1337 proxy_arp_set[f->unit] = 1;
1340 demand_rexmit(PPP_IP);
1341 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1345 * Set IP addresses and (if specified) netmask.
1347 mask = GetMask(go->ouraddr);
1349 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1350 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1351 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1352 ipcp_close(f->unit, "Interface configuration failed");
1357 /* bring the interface up for IP */
1358 if (!sifup(f->unit)) {
1359 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1360 ipcp_close(f->unit, "Interface configuration failed");
1364 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1365 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1366 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1367 ipcp_close(f->unit, "Interface configuration failed");
1371 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1373 /* assign a default route through the interface if required */
1374 if (ipcp_wantoptions[f->unit].default_route)
1375 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1376 default_route_set[f->unit] = 1;
1378 /* Make a proxy ARP entry if requested. */
1379 if (ipcp_wantoptions[f->unit].proxy_arp)
1380 if (sifproxyarp(f->unit, ho->hisaddr))
1381 proxy_arp_set[f->unit] = 1;
1383 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1384 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1391 * ipcp_down - IPCP has gone DOWN.
1393 * Take the IP network interface down, clear its addresses
1394 * and delete routes through it.
1400 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1401 np_down(f->unit, PPP_IP);
1402 sifvjcomp(f->unit, 0, 0, 0);
1405 * If we are doing dial-on-demand, set the interface
1406 * to queue up outgoing packets (for now).
1409 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1412 ipcp_clear_addrs(f->unit);
1419 * ipcp_clear_addrs() - clear the interface addresses, routes,
1420 * proxy arp entries, etc.
1423 ipcp_clear_addrs(unit)
1426 u_int32_t ouraddr, hisaddr;
1428 ouraddr = ipcp_gotoptions[unit].ouraddr;
1429 hisaddr = ipcp_hisoptions[unit].hisaddr;
1430 if (proxy_arp_set[unit]) {
1431 cifproxyarp(unit, hisaddr);
1432 proxy_arp_set[unit] = 0;
1434 if (default_route_set[unit]) {
1435 cifdefaultroute(unit, ouraddr, hisaddr);
1436 default_route_set[unit] = 0;
1438 cifaddr(unit, ouraddr, hisaddr);
1443 * ipcp_finished - possibly shut down the lower layers.
1449 np_finished(f->unit, PPP_IP);
1453 * ipcp_printpkt - print the contents of an IPCP packet.
1455 static char *ipcp_codenames[] = {
1456 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1457 "TermReq", "TermAck", "CodeRej"
1461 ipcp_printpkt(p, plen, printer, arg)
1464 void (*printer) __P((void *, char *, ...));
1467 int code, id, len, olen;
1468 u_char *pstart, *optend;
1472 if (plen < HEADERLEN)
1478 if (len < HEADERLEN || len > plen)
1481 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1482 printer(arg, " %s", ipcp_codenames[code-1]);
1484 printer(arg, " code=0x%x", code);
1485 printer(arg, " id=0x%x", id);
1492 /* print option list */
1497 if (olen < 2 || olen > len) {
1505 if (olen == CILEN_ADDRS) {
1508 printer(arg, "addrs %I", htonl(cilong));
1510 printer(arg, " %I", htonl(cilong));
1513 case CI_COMPRESSTYPE:
1514 if (olen >= CILEN_COMPRESS) {
1516 GETSHORT(cishort, p);
1517 printer(arg, "compress ");
1522 case IPCP_VJ_COMP_OLD:
1523 printer(arg, "old-VJ");
1526 printer(arg, "0x%x", cishort);
1531 if (olen == CILEN_ADDR) {
1534 printer(arg, "addr %I", htonl(cilong));
1541 printer(arg, "ms-dns %I", htonl(cilong));
1547 printer(arg, "ms-wins %I", htonl(cilong));
1550 while (p < optend) {
1552 printer(arg, " %.2x", code);
1560 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1562 print_string(p, len, printer, arg);
1569 /* print the rest of the bytes in the packet */
1570 for (; len > 0; --len) {
1572 printer(arg, " %.2x", code);
1579 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1580 * We don't bring the link up for IP fragments or for TCP FIN packets
1583 #define IP_HDRLEN 20 /* bytes */
1584 #define IP_OFFMASK 0x1fff
1585 #define IPPROTO_TCP 6
1586 #define TCP_HDRLEN 20
1590 * We use these macros because the IP header may be at an odd address,
1591 * and some compilers might use word loads to get th_off or ip_hl.
1594 #define net_short(x) (((x)[0] << 8) + (x)[1])
1595 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1596 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1597 #define get_ipproto(x) (((unsigned char *)(x))[9])
1598 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1599 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1602 ip_active_pkt(pkt, len)
1611 if (len < IP_HDRLEN)
1613 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1615 if (get_ipproto(pkt) != IPPROTO_TCP)
1617 hlen = get_iphl(pkt) * 4;
1618 if (len < hlen + TCP_HDRLEN)
1621 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)