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
247 /* max slots and slot-id compression are currently hardwired in */
248 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
253 ao->maxslotindex = MAX_STATES - 1;
257 * XXX These control whether the user may use the proxyarp
258 * and defaultroute options.
261 ao->default_route = 1;
266 * ipcp_open - IPCP is allowed to come up.
272 fsm_open(&ipcp_fsm[unit]);
277 * ipcp_close - Take IPCP down.
280 ipcp_close(unit, reason)
284 fsm_close(&ipcp_fsm[unit], reason);
289 * ipcp_lowerup - The lower layer is up.
295 fsm_lowerup(&ipcp_fsm[unit]);
300 * ipcp_lowerdown - The lower layer is down.
306 fsm_lowerdown(&ipcp_fsm[unit]);
311 * ipcp_input - Input IPCP packet.
314 ipcp_input(unit, p, len)
319 fsm_input(&ipcp_fsm[unit], p, len);
324 * ipcp_protrej - A Protocol-Reject was received for IPCP.
326 * Pretend the lower layer went down, so we shut up.
332 fsm_lowerdown(&ipcp_fsm[unit]);
337 * ipcp_resetci - Reset our CI.
343 ipcp_options *wo = &ipcp_wantoptions[f->unit];
345 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
346 if (wo->ouraddr == 0)
347 wo->accept_local = 1;
348 if (wo->hisaddr == 0)
349 wo->accept_remote = 1;
350 ipcp_gotoptions[f->unit] = *wo;
351 cis_received[f->unit] = 0;
356 * ipcp_cilen - Return length of our CI.
362 ipcp_options *go = &ipcp_gotoptions[f->unit];
363 ipcp_options *wo = &ipcp_wantoptions[f->unit];
364 ipcp_options *ho = &ipcp_hisoptions[f->unit];
367 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
368 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
371 * First see if we want to change our options to the old
372 * forms because we have received old forms from the peer.
374 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
375 /* use the old style of address negotiation */
379 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
380 /* try an older style of VJ negotiation */
381 if (cis_received[f->unit] == 0) {
382 #ifndef PPP_INHIBIT_OLD_VJ_COMPRESSION
383 /* keep trying the new style until we see some CI from the peer */
387 /* use the old style only if the peer did */
388 if (ho->neg_vj && ho->old_vj) {
391 go->vj_protocol = ho->vj_protocol;
396 len = LENCIADDR(go->neg_addr, go->old_addrs) +
397 LENCIVJ(go->neg_vj, go->old_vj);
399 #ifdef CYGOPT_PPP_NS_NEGOTIATE
400 len += LENCIADDR(go->neg_dns1, 0) +
401 LENCIADDR(go->neg_wins1, 0) +
402 LENCIADDR(go->neg_dns2, 0) +
403 LENCIADDR(go->neg_wins2, 0);
410 * ipcp_addci - Add our desired CIs to a packet.
413 ipcp_addci(f, ucp, lenp)
418 ipcp_options *go = &ipcp_gotoptions[f->unit];
421 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
423 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
424 if (len >= vjlen) { \
426 PUTCHAR(vjlen, ucp); \
427 PUTSHORT(val, ucp); \
429 PUTCHAR(maxslotindex, ucp); \
430 PUTCHAR(cflag, ucp); \
437 #define ADDCIADDR(opt, neg, old, val1, val2) \
439 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
440 if (len >= addrlen) { \
443 PUTCHAR(addrlen, ucp); \
455 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
456 go->old_addrs, go->ouraddr, go->hisaddr);
458 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
459 go->maxslotindex, go->cflag);
461 #ifdef CYGOPT_PPP_NS_NEGOTIATE
462 ADDCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
463 ADDCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
464 ADDCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
465 ADDCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
473 * ipcp_ackci - Ack our CIs.
480 ipcp_ackci(f, p, len)
485 ipcp_options *go = &ipcp_gotoptions[f->unit];
486 u_short cilen, citype, cishort;
488 u_char cimaxslotindex, cicflag;
491 * CIs must be in exactly the same order that we sent...
492 * Check packet length and CI length at each step.
493 * If we find any deviations, then this packet is bad.
496 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
498 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
499 if ((len -= vjlen) < 0) \
501 GETCHAR(citype, p); \
503 if (cilen != vjlen || \
506 GETSHORT(cishort, p); \
507 if (cishort != val) \
510 GETCHAR(cimaxslotindex, p); \
511 if (cimaxslotindex != maxslotindex) \
513 GETCHAR(cicflag, p); \
514 if (cicflag != cflag) \
519 #define ACKCIADDR(opt, neg, old, val1, val2) \
521 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
523 if ((len -= addrlen) < 0) \
525 GETCHAR(citype, p); \
527 if (cilen != addrlen || \
532 if (val1 != cilong) \
537 if (val2 != cilong) \
542 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
543 go->old_addrs, go->ouraddr, go->hisaddr);
545 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
546 go->maxslotindex, go->cflag);
548 #ifdef CYGOPT_PPP_NS_NEGOTIATE
549 ACKCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
550 ACKCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
551 ACKCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
552 ACKCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
556 * If there are any remaining CIs, then this packet is bad.
563 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
568 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
569 * This should not modify any state if the Nak is bad
570 * or if IPCP is in the OPENED state.
577 ipcp_nakci(f, p, len)
582 ipcp_options *go = &ipcp_gotoptions[f->unit];
583 u_char cimaxslotindex, cicflag;
584 u_char citype, cilen, *next;
586 u_int32_t ciaddr1, ciaddr2, l;
587 ipcp_options no; /* options we've seen Naks for */
588 ipcp_options try; /* options to request next time */
590 BZERO(&no, sizeof(no));
594 * Any Nak'd CIs must be in exactly the same order that we sent.
595 * Check packet length and CI length at each step.
596 * If we find any deviations, then this packet is bad.
598 #define NAKCIADDR(opt, neg, old, code) \
600 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
606 ciaddr1 = htonl(l); \
609 ciaddr2 = htonl(l); \
617 #define NAKCIVJ(opt, neg, code) \
619 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
624 GETSHORT(cishort, p); \
630 * Accept the peer's idea of {our,his} address, if different
631 * from our idea, only if the accept_{local,remote} flag is set.
633 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
634 if (go->accept_local && ciaddr1) { /* Do we know our address? */
635 try.ouraddr = ciaddr1;
636 IPCPDEBUG((LOG_INFO, "local IP address %s",
639 if (go->accept_remote && ciaddr2) { /* Does he know his? */
640 try.hisaddr = ciaddr2;
641 IPCPDEBUG((LOG_INFO, "remote IP address %s",
647 * Accept the peer's value of maxslotindex provided that it
648 * is less than what we asked for. Turn off slot-ID compression
649 * if the peer wants. Send old-style compress-type option if
652 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
653 if (cilen == CILEN_VJ) {
654 GETCHAR(cimaxslotindex, p);
656 if (cishort == IPCP_VJ_COMP) {
658 if (cimaxslotindex < go->maxslotindex)
659 try.maxslotindex = cimaxslotindex;
666 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
668 try.vj_protocol = cishort;
675 #ifdef CYGOPT_PPP_NS_NEGOTIATE
677 * Accept the peer's idea of the DNS and WINS addresses
679 NAKCIADDR(CI_MS_DNS1, neg_dns1, 0, try.dnsaddr[0] = ciaddr1;);
680 NAKCIADDR(CI_MS_WINS1, neg_wins1, 0, try.winsaddr[0] = ciaddr1;);
681 NAKCIADDR(CI_MS_DNS2, neg_dns2, 0, try.dnsaddr[1] = ciaddr1;);
682 NAKCIADDR(CI_MS_WINS2, neg_wins2, 0, try.winsaddr[1] = ciaddr1;);
683 #ifdef CYGOPT_PPP_DNS_CONFIGURE
684 if (try.dnsaddr[0] != 0 )
686 cyg_dns_res_init((struct in_addr *)&try.dnsaddr[0]);
692 * There may be remaining CIs, if the peer is requesting negotiation
693 * on an option that we didn't include in our request packet.
694 * If they want to negotiate about IP addresses, we comply.
695 * If they want us to ask for compression, we refuse.
697 while (len > CILEN_VOID) {
700 if( (len -= cilen) < 0 )
702 next = p + cilen - 2;
705 case CI_COMPRESSTYPE:
706 if (go->neg_vj || no.neg_vj ||
707 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
712 if ((go->neg_addr && go->old_addrs) || no.old_addrs
713 || cilen != CILEN_ADDRS)
719 if (ciaddr1 && go->accept_local)
720 try.ouraddr = ciaddr1;
723 if (ciaddr2 && go->accept_remote)
724 try.hisaddr = ciaddr2;
728 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
733 if (ciaddr1 && go->accept_local)
734 try.ouraddr = ciaddr1;
735 if (try.ouraddr != 0)
743 /* If there is still anything left, this packet is bad. */
748 * OK, the Nak is good. Now we can update state.
750 if (f->state != OPENED)
756 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
762 * ipcp_rejci - Reject some of our CIs.
765 ipcp_rejci(f, p, len)
770 ipcp_options *go = &ipcp_gotoptions[f->unit];
771 u_char cimaxslotindex, ciflag, cilen;
774 ipcp_options try; /* options to request next time */
775 #ifdef CYGOPT_PPP_NS_NEGOTIATE
776 u_char citype, *next;
781 * Any Rejected CIs must be in exactly the same order that we sent.
782 * Check packet length and CI length at each step.
783 * If we find any deviations, then this packet is bad.
785 #define REJCIADDR(opt, neg, old, val1, val2) \
787 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
795 /* Check rejected value. */ \
796 if (cilong != val1) \
801 /* Check rejected value. */ \
802 if (cilong != val2) \
808 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
810 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
815 GETSHORT(cishort, p); \
816 /* Check rejected value. */ \
817 if (cishort != val) \
820 GETCHAR(cimaxslotindex, p); \
821 if (cimaxslotindex != maxslot) \
823 GETCHAR(ciflag, p); \
824 if (ciflag != cflag) \
830 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
831 go->old_addrs, go->ouraddr, go->hisaddr);
833 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
834 go->maxslotindex, go->cflag);
836 #ifdef CYGOPT_PPP_NS_NEGOTIATE
838 * There may be remaining CIs, if the peer is unable to support
839 * DNS or WINS negotiation. If so, just turn them off
843 while (len > CILEN_VOID) {
846 if( (len -= cilen) < 0 )
848 next = p + cilen - 2;
869 * If there are any remaining CIs, then this packet is bad.
874 * Now we can update state.
876 if (f->state != OPENED)
881 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
887 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
889 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
890 * appropriately. If reject_if_disagree is non-zero, doesn't return
891 * CONFNAK; returns CONFREJ if it can't return CONFACK.
894 ipcp_reqci(f, inp, len, reject_if_disagree)
896 u_char *inp; /* Requested CIs */
897 int *len; /* Length of requested CIs */
898 int reject_if_disagree;
900 ipcp_options *wo = &ipcp_wantoptions[f->unit];
901 ipcp_options *ho = &ipcp_hisoptions[f->unit];
902 ipcp_options *ao = &ipcp_allowoptions[f->unit];
903 ipcp_options *go = &ipcp_gotoptions[f->unit];
904 u_char *cip, *next; /* Pointer to current and next CIs */
905 u_short cilen, citype; /* Parsed len, type */
906 u_short cishort; /* Parsed short value */
907 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
908 int rc = CONFACK; /* Final packet return code */
909 int orc; /* Individual option return code */
910 u_char *p; /* Pointer to next char to parse */
911 u_char *ucp = inp; /* Pointer to current output char */
912 int l = *len; /* Length left */
913 u_char maxslotindex, cflag;
916 cis_received[f->unit] = 1;
919 * Reset all his options.
921 BZERO(ho, sizeof(*ho));
924 * Process all his options.
928 orc = CONFACK; /* Assume success */
929 cip = p = next; /* Remember begining of CI */
930 if (l < 2 || /* Not enough data for CI header or */
931 p[1] < 2 || /* CI length too small or */
932 p[1] > l) { /* CI length too big? */
933 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
934 orc = CONFREJ; /* Reject bad CI */
935 cilen = l; /* Reject till end of packet */
936 l = 0; /* Don't loop again */
939 GETCHAR(citype, p); /* Parse CI type */
940 GETCHAR(cilen, p); /* Parse CI length */
941 l -= cilen; /* Adjust remaining length */
942 next += cilen; /* Step to next CI */
944 switch (citype) { /* Check CI type */
946 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
948 cilen != CILEN_ADDRS) { /* Check CI length */
949 orc = CONFREJ; /* Reject CI */
954 * If he has no address, or if we both have his address but
955 * disagree about it, then NAK it with our idea.
956 * In particular, if we don't know his address, but he does,
959 GETLONG(tl, p); /* Parse source address (his) */
961 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
962 if (ciaddr1 != wo->hisaddr
963 && (ciaddr1 == 0 || !wo->accept_remote)) {
965 if (!reject_if_disagree) {
966 DECPTR(sizeof(u_int32_t), p);
967 tl = ntohl(wo->hisaddr);
970 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
972 * If neither we nor he knows his address, reject the option.
975 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
980 * If he doesn't know our address, or if we both have our address
981 * but disagree about it, then NAK it with our idea.
983 GETLONG(tl, p); /* Parse desination address (ours) */
985 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
986 if (ciaddr2 != wo->ouraddr) {
987 if (ciaddr2 == 0 || !wo->accept_local) {
989 if (!reject_if_disagree) {
990 DECPTR(sizeof(u_int32_t), p);
991 tl = ntohl(wo->ouraddr);
995 go->ouraddr = ciaddr2; /* accept peer's idea */
1001 ho->hisaddr = ciaddr1;
1002 ho->ouraddr = ciaddr2;
1006 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
1008 if (!ao->neg_addr ||
1009 cilen != CILEN_ADDR) { /* Check CI length */
1010 orc = CONFREJ; /* Reject CI */
1015 * If he has no address, or if we both have his address but
1016 * disagree about it, then NAK it with our idea.
1017 * In particular, if we don't know his address, but he does,
1020 GETLONG(tl, p); /* Parse source address (his) */
1021 ciaddr1 = htonl(tl);
1022 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
1023 if (ciaddr1 != wo->hisaddr
1024 && (ciaddr1 == 0 || !wo->accept_remote)) {
1026 if (!reject_if_disagree) {
1027 DECPTR(sizeof(u_int32_t), p);
1028 tl = ntohl(wo->hisaddr);
1031 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1033 * Don't ACK an address of 0.0.0.0 - reject it instead.
1036 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1041 ho->hisaddr = ciaddr1;
1046 /* Microsoft primary or secondary DNS request */
1047 d = citype == CI_MS_DNS2;
1048 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
1050 /* If we do not have a DNS address then we cannot send it */
1051 if (ao->dnsaddr[d] == 0 ||
1052 cilen != CILEN_ADDR) { /* Check CI length */
1053 orc = CONFREJ; /* Reject CI */
1057 if (htonl(tl) != ao->dnsaddr[d]) {
1058 DECPTR(sizeof(u_int32_t), p);
1059 tl = ntohl(ao->dnsaddr[d]);
1067 /* Microsoft primary or secondary WINS request */
1068 d = citype == CI_MS_WINS2;
1069 IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
1071 /* If we do not have a DNS address then we cannot send it */
1072 if (ao->winsaddr[d] == 0 ||
1073 cilen != CILEN_ADDR) { /* Check CI length */
1074 orc = CONFREJ; /* Reject CI */
1078 if (htonl(tl) != ao->winsaddr[d]) {
1079 DECPTR(sizeof(u_int32_t), p);
1080 tl = ntohl(ao->winsaddr[d]);
1086 case CI_COMPRESSTYPE:
1087 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
1089 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1093 GETSHORT(cishort, p);
1094 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
1096 if (!(cishort == IPCP_VJ_COMP ||
1097 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1103 ho->vj_protocol = cishort;
1104 if (cilen == CILEN_VJ) {
1105 GETCHAR(maxslotindex, p);
1106 if (maxslotindex > ao->maxslotindex) {
1108 if (!reject_if_disagree){
1110 PUTCHAR(ao->maxslotindex, p);
1114 if (cflag && !ao->cflag) {
1116 if (!reject_if_disagree){
1118 PUTCHAR(wo->cflag, p);
1121 ho->maxslotindex = maxslotindex;
1125 ho->maxslotindex = MAX_STATES - 1;
1136 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
1138 if (orc == CONFACK && /* Good CI */
1139 rc != CONFACK) /* but prior CI wasnt? */
1140 continue; /* Don't send this one */
1142 if (orc == CONFNAK) { /* Nak this CI? */
1143 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1144 orc = CONFREJ; /* Get tough if so */
1146 if (rc == CONFREJ) /* Rejecting prior CI? */
1147 continue; /* Don't send this one */
1148 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1149 rc = CONFNAK; /* Not anymore... */
1150 ucp = inp; /* Backup */
1155 if (orc == CONFREJ && /* Reject this CI */
1156 rc != CONFREJ) { /* but no prior ones? */
1158 ucp = inp; /* Backup */
1161 /* Need to move CI? */
1163 BCOPY(cip, ucp, cilen); /* Move it */
1165 /* Update output pointer */
1170 * If we aren't rejecting this packet, and we want to negotiate
1171 * their address, and they didn't send their address, then we
1172 * send a NAK with a CI_ADDR option appended. We assume the
1173 * input buffer is long enough that we can append the extra
1176 if (rc != CONFREJ && !ho->neg_addr &&
1177 wo->req_addr && !reject_if_disagree) {
1178 if (rc == CONFACK) {
1180 ucp = inp; /* reset pointer */
1181 wo->req_addr = 0; /* don't ask again */
1183 PUTCHAR(CI_ADDR, ucp);
1184 PUTCHAR(CILEN_ADDR, ucp);
1185 tl = ntohl(wo->hisaddr);
1189 *len = ucp - inp; /* Compute output length */
1190 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1191 return (rc); /* Return final code */
1196 * ip_check_options - check that any IP-related options are OK,
1197 * and assign appropriate defaults.
1204 ipcp_options *wo = &ipcp_wantoptions[0];
1207 * Default our local IP address based on our hostname.
1208 * If local IP address already given, don't bother.
1210 if (wo->ouraddr == 0 && !disable_defaultip) {
1212 * Look up our hostname (possibly with domain name appended)
1213 * and take the first IP address as our local IP address.
1214 * If there isn't an IP address for our hostname, too bad.
1216 wo->accept_local = 1; /* don't insist on this default value */
1217 if ((hp = gethostbyname(cyg_ppp_hostname)) != NULL) {
1218 local = *(u_int32_t *)hp->h_addr;
1219 if (local != 0 && !bad_ip_adrs(local))
1220 wo->ouraddr = local;
1224 if (demand && wo->hisaddr == 0) {
1225 option_error("remote IP address required for demand-dialling\n");
1231 * ip_demand_conf - configure the interface as though
1232 * IPCP were up, for use with dial-on-demand.
1238 ipcp_options *wo = &ipcp_wantoptions[u];
1240 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1244 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1246 if (wo->default_route)
1247 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1248 default_route_set[u] = 1;
1250 if (sifproxyarp(u, wo->hisaddr))
1251 proxy_arp_set[u] = 1;
1253 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1254 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1261 * ipcp_up - IPCP has come UP.
1263 * Configure the IP network interface appropriately and bring it up.
1270 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1271 ipcp_options *go = &ipcp_gotoptions[f->unit];
1272 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1274 np_up(f->unit, PPP_IP);
1275 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1278 * We must have a non-zero IP address for both ends of the link.
1281 ho->hisaddr = wo->hisaddr;
1283 if (ho->hisaddr == 0) {
1284 syslog(LOG_ERR, "Could not determine remote IP address");
1285 ipcp_close(f->unit, "Could not determine remote IP address");
1288 if (go->ouraddr == 0) {
1289 syslog(LOG_ERR, "Could not determine local IP address");
1290 ipcp_close(f->unit, "Could not determine local IP address");
1295 * Check that the peer is allowed to use the IP address it wants.
1297 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1298 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1299 ip_ntoa(ho->hisaddr));
1300 ipcp_close(f->unit, "Unauthorized remote IP address");
1304 /* set tcp compression */
1305 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1308 * If we are doing dial-on-demand, the interface is already
1309 * configured, so we put out any saved-up packets, then set the
1310 * interface to pass IP packets.
1313 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1314 if (go->ouraddr != wo->ouraddr)
1315 syslog(LOG_WARNING, "Local IP address changed to %s",
1316 ip_ntoa(go->ouraddr));
1317 if (ho->hisaddr != wo->hisaddr)
1318 syslog(LOG_WARNING, "Remote IP address changed to %s",
1319 ip_ntoa(ho->hisaddr));
1320 ipcp_clear_addrs(f->unit);
1322 /* Set the interface to the new addresses */
1323 mask = GetMask(go->ouraddr);
1324 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1325 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1326 ipcp_close(f->unit, "Interface configuration failed");
1330 /* assign a default route through the interface if required */
1331 if (ipcp_wantoptions[f->unit].default_route)
1332 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1333 default_route_set[f->unit] = 1;
1335 /* Make a proxy ARP entry if requested. */
1336 if (ipcp_wantoptions[f->unit].proxy_arp)
1337 if (sifproxyarp(f->unit, ho->hisaddr))
1338 proxy_arp_set[f->unit] = 1;
1341 demand_rexmit(PPP_IP);
1342 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1346 * Set IP addresses and (if specified) netmask.
1348 mask = GetMask(go->ouraddr);
1350 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1351 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1352 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1353 ipcp_close(f->unit, "Interface configuration failed");
1358 /* bring the interface up for IP */
1359 if (!sifup(f->unit)) {
1360 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1361 ipcp_close(f->unit, "Interface configuration failed");
1365 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1366 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1367 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1368 ipcp_close(f->unit, "Interface configuration failed");
1372 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1374 /* assign a default route through the interface if required */
1375 if (ipcp_wantoptions[f->unit].default_route)
1376 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1377 default_route_set[f->unit] = 1;
1379 /* Make a proxy ARP entry if requested. */
1380 if (ipcp_wantoptions[f->unit].proxy_arp)
1381 if (sifproxyarp(f->unit, ho->hisaddr))
1382 proxy_arp_set[f->unit] = 1;
1384 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1385 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1392 * ipcp_down - IPCP has gone DOWN.
1394 * Take the IP network interface down, clear its addresses
1395 * and delete routes through it.
1401 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1402 np_down(f->unit, PPP_IP);
1403 sifvjcomp(f->unit, 0, 0, 0);
1406 * If we are doing dial-on-demand, set the interface
1407 * to queue up outgoing packets (for now).
1410 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1413 ipcp_clear_addrs(f->unit);
1420 * ipcp_clear_addrs() - clear the interface addresses, routes,
1421 * proxy arp entries, etc.
1424 ipcp_clear_addrs(unit)
1427 u_int32_t ouraddr, hisaddr;
1429 ouraddr = ipcp_gotoptions[unit].ouraddr;
1430 hisaddr = ipcp_hisoptions[unit].hisaddr;
1431 if (proxy_arp_set[unit]) {
1432 cifproxyarp(unit, hisaddr);
1433 proxy_arp_set[unit] = 0;
1435 if (default_route_set[unit]) {
1436 cifdefaultroute(unit, ouraddr, hisaddr);
1437 default_route_set[unit] = 0;
1439 cifaddr(unit, ouraddr, hisaddr);
1444 * ipcp_finished - possibly shut down the lower layers.
1450 np_finished(f->unit, PPP_IP);
1454 * ipcp_printpkt - print the contents of an IPCP packet.
1456 static char *ipcp_codenames[] = {
1457 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1458 "TermReq", "TermAck", "CodeRej"
1462 ipcp_printpkt(p, plen, printer, arg)
1465 void (*printer) __P((void *, char *, ...));
1468 int code, id, len, olen;
1469 u_char *pstart, *optend;
1473 if (plen < HEADERLEN)
1479 if (len < HEADERLEN || len > plen)
1482 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1483 printer(arg, " %s", ipcp_codenames[code-1]);
1485 printer(arg, " code=0x%x", code);
1486 printer(arg, " id=0x%x", id);
1493 /* print option list */
1498 if (olen < 2 || olen > len) {
1506 if (olen == CILEN_ADDRS) {
1509 printer(arg, "addrs %I", htonl(cilong));
1511 printer(arg, " %I", htonl(cilong));
1514 case CI_COMPRESSTYPE:
1515 if (olen >= CILEN_COMPRESS) {
1517 GETSHORT(cishort, p);
1518 printer(arg, "compress ");
1523 case IPCP_VJ_COMP_OLD:
1524 printer(arg, "old-VJ");
1527 printer(arg, "0x%x", cishort);
1532 if (olen == CILEN_ADDR) {
1535 printer(arg, "addr %I", htonl(cilong));
1542 printer(arg, "ms-dns %I", htonl(cilong));
1548 printer(arg, "ms-wins %I", htonl(cilong));
1551 while (p < optend) {
1553 printer(arg, " %.2x", code);
1561 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1563 print_string(p, len, printer, arg);
1570 /* print the rest of the bytes in the packet */
1571 for (; len > 0; --len) {
1573 printer(arg, " %.2x", code);
1580 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1581 * We don't bring the link up for IP fragments or for TCP FIN packets
1584 #define IP_HDRLEN 20 /* bytes */
1585 #define IP_OFFMASK 0x1fff
1586 #define IPPROTO_TCP 6
1587 #define TCP_HDRLEN 20
1591 * We use these macros because the IP header may be at an odd address,
1592 * and some compilers might use word loads to get th_off or ip_hl.
1595 #define net_short(x) (((x)[0] << 8) + (x)[1])
1596 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1597 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1598 #define get_ipproto(x) (((unsigned char *)(x))[9])
1599 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1600 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1603 ip_active_pkt(pkt, len)
1612 if (len < IP_HDRLEN)
1614 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1616 if (get_ipproto(pkt) != IPPROTO_TCP)
1618 hlen = get_iphl(pkt) * 4;
1619 if (len < hlen + TCP_HDRLEN)
1622 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)