2 * ipcp.c - PPP IP Control Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 static char rcsid[] = "$Id: ipcp.c,v 1.27 1996/07/01 05:32:17 paulus Exp $";
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
43 #include "pathnames.h"
46 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
47 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
48 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
49 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
52 static int cis_received[NUM_PPP]; /* # Conf-Reqs received */
53 static int default_route_set[NUM_PPP]; /* Have set up a default route */
54 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
57 * Callbacks for fsm code. (CI = Configuration Information)
59 static void ipcp_resetci __P((fsm *)); /* Reset our CI */
60 static int ipcp_cilen __P((fsm *)); /* Return length of our CI */
61 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
62 static int ipcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
63 static int ipcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
64 static int ipcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
65 static int ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
66 static void ipcp_up __P((fsm *)); /* We're UP */
67 static void ipcp_down __P((fsm *)); /* We're DOWN */
68 static void ipcp_script __P((fsm *, char *)); /* Run an up/down script */
69 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
71 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
73 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
74 ipcp_resetci, /* Reset our Configuration Information */
75 ipcp_cilen, /* Length of our Configuration Information */
76 ipcp_addci, /* Add our Configuration Information */
77 ipcp_ackci, /* ACK our Configuration Information */
78 ipcp_nakci, /* NAK our Configuration Information */
79 ipcp_rejci, /* Reject our Configuration Information */
80 ipcp_reqci, /* Request peer's Configuration Information */
81 ipcp_up, /* Called when fsm reaches OPENED state */
82 ipcp_down, /* Called when fsm leaves OPENED state */
83 NULL, /* Called when we want the lower layer up */
84 ipcp_finished, /* Called when we want the lower layer down */
85 NULL, /* Called when Protocol-Reject received */
86 NULL, /* Retransmission is necessary */
87 NULL, /* Called to handle protocol-specific codes */
88 "IPCP" /* String name of protocol */
92 * Protocol entry points from main code.
94 static void ipcp_init __P((int));
95 static void ipcp_open __P((int));
96 static void ipcp_close __P((int, char *));
97 static void ipcp_lowerup __P((int));
98 static void ipcp_lowerdown __P((int));
99 static void ipcp_input __P((int, u_char *, int));
100 static void ipcp_protrej __P((int));
101 static int ipcp_printpkt __P((u_char *, int,
102 void (*) __P((void *, char *, ...)), void *));
103 static void ip_check_options __P((void));
104 static int ip_demand_conf __P((int));
105 static int ip_active_pkt __P((u_char *, int));
107 struct protent ipcp_protent = {
126 * Lengths of configuration options.
129 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
130 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
131 #define CILEN_ADDR 6 /* new-style single address option */
132 #define CILEN_ADDRS 10 /* old-style dual address option */
135 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
136 (x) == CONFNAK ? "NAK" : "REJ")
140 * Make a string representation of a network IP address.
148 ipaddr = ntohl(ipaddr);
150 sprintf(b, "%d.%d.%d.%d",
151 (u_char)(ipaddr >> 24),
152 (u_char)(ipaddr >> 16),
153 (u_char)(ipaddr >> 8),
160 * ipcp_init - Initialize IPCP.
166 fsm *f = &ipcp_fsm[unit];
167 ipcp_options *wo = &ipcp_wantoptions[unit];
168 ipcp_options *ao = &ipcp_allowoptions[unit];
171 f->protocol = PPP_IPCP;
172 f->callbacks = &ipcp_callbacks;
173 fsm_init(&ipcp_fsm[unit]);
175 memset(wo, 0, sizeof(*wo));
176 memset(ao, 0, sizeof(*ao));
180 wo->vj_protocol = IPCP_VJ_COMP;
181 wo->maxslotindex = MAX_STATES - 1; /* really max index */
184 /* max slots and slot-id compression are currently hardwired in */
185 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
190 ao->maxslotindex = MAX_STATES - 1;
194 * XXX These control whether the user may use the proxyarp
195 * and defaultroute options.
198 ao->default_route = 1;
203 * ipcp_open - IPCP is allowed to come up.
209 fsm_open(&ipcp_fsm[unit]);
214 * ipcp_close - Take IPCP down.
217 ipcp_close(unit, reason)
221 fsm_close(&ipcp_fsm[unit], reason);
226 * ipcp_lowerup - The lower layer is up.
232 fsm_lowerup(&ipcp_fsm[unit]);
237 * ipcp_lowerdown - The lower layer is down.
243 fsm_lowerdown(&ipcp_fsm[unit]);
248 * ipcp_input - Input IPCP packet.
251 ipcp_input(unit, p, len)
256 fsm_input(&ipcp_fsm[unit], p, len);
261 * ipcp_protrej - A Protocol-Reject was received for IPCP.
263 * Pretend the lower layer went down, so we shut up.
269 fsm_lowerdown(&ipcp_fsm[unit]);
274 * ipcp_resetci - Reset our CI.
280 ipcp_options *wo = &ipcp_wantoptions[f->unit];
282 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
283 if (wo->ouraddr == 0)
284 wo->accept_local = 1;
285 if (wo->hisaddr == 0)
286 wo->accept_remote = 1;
287 ipcp_gotoptions[f->unit] = *wo;
288 cis_received[f->unit] = 0;
293 * ipcp_cilen - Return length of our CI.
299 ipcp_options *go = &ipcp_gotoptions[f->unit];
300 ipcp_options *wo = &ipcp_wantoptions[f->unit];
301 ipcp_options *ho = &ipcp_hisoptions[f->unit];
303 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
304 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
307 * First see if we want to change our options to the old
308 * forms because we have received old forms from the peer.
310 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
311 /* use the old style of address negotiation */
315 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
316 /* try an older style of VJ negotiation */
317 if (cis_received[f->unit] == 0) {
318 /* keep trying the new style until we see some CI from the peer */
321 /* use the old style only if the peer did */
322 if (ho->neg_vj && ho->old_vj) {
325 go->vj_protocol = ho->vj_protocol;
330 return (LENCIADDR(go->neg_addr, go->old_addrs) +
331 LENCIVJ(go->neg_vj, go->old_vj));
336 * ipcp_addci - Add our desired CIs to a packet.
339 ipcp_addci(f, ucp, lenp)
344 ipcp_options *go = &ipcp_gotoptions[f->unit];
347 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
349 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
350 if (len >= vjlen) { \
352 PUTCHAR(vjlen, ucp); \
353 PUTSHORT(val, ucp); \
355 PUTCHAR(maxslotindex, ucp); \
356 PUTCHAR(cflag, ucp); \
363 #define ADDCIADDR(opt, neg, old, val1, val2) \
365 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
366 if (len >= addrlen) { \
369 PUTCHAR(addrlen, ucp); \
381 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
382 go->old_addrs, go->ouraddr, go->hisaddr);
384 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
385 go->maxslotindex, go->cflag);
392 * ipcp_ackci - Ack our CIs.
399 ipcp_ackci(f, p, len)
404 ipcp_options *go = &ipcp_gotoptions[f->unit];
405 u_short cilen, citype, cishort;
407 u_char cimaxslotindex, cicflag;
410 * CIs must be in exactly the same order that we sent...
411 * Check packet length and CI length at each step.
412 * If we find any deviations, then this packet is bad.
415 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
417 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
418 if ((len -= vjlen) < 0) \
420 GETCHAR(citype, p); \
422 if (cilen != vjlen || \
425 GETSHORT(cishort, p); \
426 if (cishort != val) \
429 GETCHAR(cimaxslotindex, p); \
430 if (cimaxslotindex != maxslotindex) \
432 GETCHAR(cicflag, p); \
433 if (cicflag != cflag) \
438 #define ACKCIADDR(opt, neg, old, val1, val2) \
440 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
442 if ((len -= addrlen) < 0) \
444 GETCHAR(citype, p); \
446 if (cilen != addrlen || \
451 if (val1 != cilong) \
456 if (val2 != cilong) \
461 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
462 go->old_addrs, go->ouraddr, go->hisaddr);
464 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
465 go->maxslotindex, go->cflag);
468 * If there are any remaining CIs, then this packet is bad.
475 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
480 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
481 * This should not modify any state if the Nak is bad
482 * or if IPCP is in the OPENED state.
489 ipcp_nakci(f, p, len)
494 ipcp_options *go = &ipcp_gotoptions[f->unit];
495 u_char cimaxslotindex, cicflag;
496 u_char citype, cilen, *next;
498 u_int32_t ciaddr1, ciaddr2, l;
499 ipcp_options no; /* options we've seen Naks for */
500 ipcp_options try; /* options to request next time */
502 BZERO(&no, sizeof(no));
506 * Any Nak'd CIs must be in exactly the same order that we sent.
507 * Check packet length and CI length at each step.
508 * If we find any deviations, then this packet is bad.
510 #define NAKCIADDR(opt, neg, old, code) \
512 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
518 ciaddr1 = htonl(l); \
521 ciaddr2 = htonl(l); \
529 #define NAKCIVJ(opt, neg, code) \
531 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
536 GETSHORT(cishort, p); \
542 * Accept the peer's idea of {our,his} address, if different
543 * from our idea, only if the accept_{local,remote} flag is set.
545 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
546 if (go->accept_local && ciaddr1) { /* Do we know our address? */
547 try.ouraddr = ciaddr1;
548 IPCPDEBUG((LOG_INFO, "local IP address %s",
551 if (go->accept_remote && ciaddr2) { /* Does he know his? */
552 try.hisaddr = ciaddr2;
553 IPCPDEBUG((LOG_INFO, "remote IP address %s",
559 * Accept the peer's value of maxslotindex provided that it
560 * is less than what we asked for. Turn off slot-ID compression
561 * if the peer wants. Send old-style compress-type option if
564 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
565 if (cilen == CILEN_VJ) {
566 GETCHAR(cimaxslotindex, p);
568 if (cishort == IPCP_VJ_COMP) {
570 if (cimaxslotindex < go->maxslotindex)
571 try.maxslotindex = cimaxslotindex;
578 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
580 try.vj_protocol = cishort;
588 * There may be remaining CIs, if the peer is requesting negotiation
589 * on an option that we didn't include in our request packet.
590 * If they want to negotiate about IP addresses, we comply.
591 * If they want us to ask for compression, we refuse.
593 while (len > CILEN_VOID) {
596 if( (len -= cilen) < 0 )
598 next = p + cilen - 2;
601 case CI_COMPRESSTYPE:
602 if (go->neg_vj || no.neg_vj ||
603 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
608 if (go->neg_addr && go->old_addrs || no.old_addrs
609 || cilen != CILEN_ADDRS)
615 if (ciaddr1 && go->accept_local)
616 try.ouraddr = ciaddr1;
619 if (ciaddr2 && go->accept_remote)
620 try.hisaddr = ciaddr2;
624 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
629 if (ciaddr1 && go->accept_local)
630 try.ouraddr = ciaddr1;
631 if (try.ouraddr != 0)
639 /* If there is still anything left, this packet is bad. */
644 * OK, the Nak is good. Now we can update state.
646 if (f->state != OPENED)
652 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
658 * ipcp_rejci - Reject some of our CIs.
661 ipcp_rejci(f, p, len)
666 ipcp_options *go = &ipcp_gotoptions[f->unit];
667 u_char cimaxslotindex, ciflag, cilen;
670 ipcp_options try; /* options to request next time */
674 * Any Rejected CIs must be in exactly the same order that we sent.
675 * Check packet length and CI length at each step.
676 * If we find any deviations, then this packet is bad.
678 #define REJCIADDR(opt, neg, old, val1, val2) \
680 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
688 /* Check rejected value. */ \
689 if (cilong != val1) \
694 /* Check rejected value. */ \
695 if (cilong != val2) \
701 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
703 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
708 GETSHORT(cishort, p); \
709 /* Check rejected value. */ \
710 if (cishort != val) \
713 GETCHAR(cimaxslotindex, p); \
714 if (cimaxslotindex != maxslot) \
716 GETCHAR(ciflag, p); \
717 if (ciflag != cflag) \
723 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
724 go->old_addrs, go->ouraddr, go->hisaddr);
726 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
727 go->maxslotindex, go->cflag);
730 * If there are any remaining CIs, then this packet is bad.
735 * Now we can update state.
737 if (f->state != OPENED)
742 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
748 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
750 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
751 * appropriately. If reject_if_disagree is non-zero, doesn't return
752 * CONFNAK; returns CONFREJ if it can't return CONFACK.
755 ipcp_reqci(f, inp, len, reject_if_disagree)
757 u_char *inp; /* Requested CIs */
758 int *len; /* Length of requested CIs */
759 int reject_if_disagree;
761 ipcp_options *wo = &ipcp_wantoptions[f->unit];
762 ipcp_options *ho = &ipcp_hisoptions[f->unit];
763 ipcp_options *ao = &ipcp_allowoptions[f->unit];
764 ipcp_options *go = &ipcp_gotoptions[f->unit];
765 u_char *cip, *next; /* Pointer to current and next CIs */
766 u_short cilen, citype; /* Parsed len, type */
767 u_short cishort; /* Parsed short value */
768 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
769 int rc = CONFACK; /* Final packet return code */
770 int orc; /* Individual option return code */
771 u_char *p; /* Pointer to next char to parse */
772 u_char *ucp = inp; /* Pointer to current output char */
773 int l = *len; /* Length left */
774 u_char maxslotindex, cflag;
777 cis_received[f->unit] = 1;
780 * Reset all his options.
782 BZERO(ho, sizeof(*ho));
785 * Process all his options.
789 orc = CONFACK; /* Assume success */
790 cip = p = next; /* Remember begining of CI */
791 if (l < 2 || /* Not enough data for CI header or */
792 p[1] < 2 || /* CI length too small or */
793 p[1] > l) { /* CI length too big? */
794 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
795 orc = CONFREJ; /* Reject bad CI */
796 cilen = l; /* Reject till end of packet */
797 l = 0; /* Don't loop again */
800 GETCHAR(citype, p); /* Parse CI type */
801 GETCHAR(cilen, p); /* Parse CI length */
802 l -= cilen; /* Adjust remaining length */
803 next += cilen; /* Step to next CI */
805 switch (citype) { /* Check CI type */
807 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
809 cilen != CILEN_ADDRS) { /* Check CI length */
810 orc = CONFREJ; /* Reject CI */
815 * If he has no address, or if we both have his address but
816 * disagree about it, then NAK it with our idea.
817 * In particular, if we don't know his address, but he does,
820 GETLONG(tl, p); /* Parse source address (his) */
822 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
823 if (ciaddr1 != wo->hisaddr
824 && (ciaddr1 == 0 || !wo->accept_remote)) {
826 if (!reject_if_disagree) {
827 DECPTR(sizeof(u_int32_t), p);
828 tl = ntohl(wo->hisaddr);
831 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
833 * If neither we nor he knows his address, reject the option.
836 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
841 * If he doesn't know our address, or if we both have our address
842 * but disagree about it, then NAK it with our idea.
844 GETLONG(tl, p); /* Parse desination address (ours) */
846 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
847 if (ciaddr2 != wo->ouraddr) {
848 if (ciaddr2 == 0 || !wo->accept_local) {
850 if (!reject_if_disagree) {
851 DECPTR(sizeof(u_int32_t), p);
852 tl = ntohl(wo->ouraddr);
856 go->ouraddr = ciaddr2; /* accept peer's idea */
862 ho->hisaddr = ciaddr1;
863 ho->ouraddr = ciaddr2;
867 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
870 cilen != CILEN_ADDR) { /* Check CI length */
871 orc = CONFREJ; /* Reject CI */
876 * If he has no address, or if we both have his address but
877 * disagree about it, then NAK it with our idea.
878 * In particular, if we don't know his address, but he does,
881 GETLONG(tl, p); /* Parse source address (his) */
883 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
884 if (ciaddr1 != wo->hisaddr
885 && (ciaddr1 == 0 || !wo->accept_remote)) {
887 if (!reject_if_disagree) {
888 DECPTR(sizeof(u_int32_t), p);
889 tl = ntohl(wo->hisaddr);
892 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
894 * Don't ACK an address of 0.0.0.0 - reject it instead.
897 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
902 ho->hisaddr = ciaddr1;
907 /* Microsoft primary or secondary DNS request */
908 d = citype == CI_MS_DNS2;
909 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
911 /* If we do not have a DNS address then we cannot send it */
912 if (ao->dnsaddr[d] == 0 ||
913 cilen != CILEN_ADDR) { /* Check CI length */
914 orc = CONFREJ; /* Reject CI */
918 if (htonl(tl) != ao->dnsaddr[d]) {
919 DECPTR(sizeof(u_int32_t), p);
920 tl = ntohl(ao->dnsaddr[d]);
926 case CI_COMPRESSTYPE:
927 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
929 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
933 GETSHORT(cishort, p);
934 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
936 if (!(cishort == IPCP_VJ_COMP ||
937 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
943 ho->vj_protocol = cishort;
944 if (cilen == CILEN_VJ) {
945 GETCHAR(maxslotindex, p);
946 if (maxslotindex > ao->maxslotindex) {
948 if (!reject_if_disagree){
950 PUTCHAR(ao->maxslotindex, p);
954 if (cflag && !ao->cflag) {
956 if (!reject_if_disagree){
958 PUTCHAR(wo->cflag, p);
961 ho->maxslotindex = maxslotindex;
965 ho->maxslotindex = MAX_STATES - 1;
976 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
978 if (orc == CONFACK && /* Good CI */
979 rc != CONFACK) /* but prior CI wasnt? */
980 continue; /* Don't send this one */
982 if (orc == CONFNAK) { /* Nak this CI? */
983 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
984 orc = CONFREJ; /* Get tough if so */
986 if (rc == CONFREJ) /* Rejecting prior CI? */
987 continue; /* Don't send this one */
988 if (rc == CONFACK) { /* Ack'd all prior CIs? */
989 rc = CONFNAK; /* Not anymore... */
990 ucp = inp; /* Backup */
995 if (orc == CONFREJ && /* Reject this CI */
996 rc != CONFREJ) { /* but no prior ones? */
998 ucp = inp; /* Backup */
1001 /* Need to move CI? */
1003 BCOPY(cip, ucp, cilen); /* Move it */
1005 /* Update output pointer */
1010 * If we aren't rejecting this packet, and we want to negotiate
1011 * their address, and they didn't send their address, then we
1012 * send a NAK with a CI_ADDR option appended. We assume the
1013 * input buffer is long enough that we can append the extra
1016 if (rc != CONFREJ && !ho->neg_addr &&
1017 wo->req_addr && !reject_if_disagree) {
1018 if (rc == CONFACK) {
1020 ucp = inp; /* reset pointer */
1021 wo->req_addr = 0; /* don't ask again */
1023 PUTCHAR(CI_ADDR, ucp);
1024 PUTCHAR(CILEN_ADDR, ucp);
1025 tl = ntohl(wo->hisaddr);
1029 *len = ucp - inp; /* Compute output length */
1030 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1031 return (rc); /* Return final code */
1036 * ip_check_options - check that any IP-related options are OK,
1037 * and assign appropriate defaults.
1044 ipcp_options *wo = &ipcp_wantoptions[0];
1047 * Default our local IP address based on our hostname.
1048 * If local IP address already given, don't bother.
1050 if (wo->ouraddr == 0 && !disable_defaultip) {
1052 * Look up our hostname (possibly with domain name appended)
1053 * and take the first IP address as our local IP address.
1054 * If there isn't an IP address for our hostname, too bad.
1056 wo->accept_local = 1; /* don't insist on this default value */
1057 if ((hp = gethostbyname(hostname)) != NULL) {
1058 local = *(u_int32_t *)hp->h_addr;
1059 if (local != 0 && !bad_ip_adrs(local))
1060 wo->ouraddr = local;
1064 if (demand && wo->hisaddr == 0) {
1065 fprintf(stderr, "%s: remote IP address required for demand-dialling\n",
1069 if (demand && wo->accept_remote) {
1070 fprintf(stderr, "%s: ipcp-accept-remote is incompatible with demand\n",
1078 * ip_demand_conf - configure the interface as though
1079 * IPCP were up, for use with dial-on-demand.
1085 ipcp_options *wo = &ipcp_wantoptions[u];
1087 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1089 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1093 if (wo->default_route)
1094 if (sifdefaultroute(u, wo->hisaddr))
1095 default_route_set[u] = 1;
1097 if (sifproxyarp(u, wo->hisaddr))
1098 proxy_arp_set[u] = 1;
1100 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1101 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1108 * ipcp_up - IPCP has come UP.
1110 * Configure the IP network interface appropriately and bring it up.
1117 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1118 ipcp_options *go = &ipcp_gotoptions[f->unit];
1119 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1121 np_up(f->unit, PPP_IP);
1122 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1125 * We must have a non-zero IP address for both ends of the link.
1128 ho->hisaddr = wo->hisaddr;
1130 if (ho->hisaddr == 0) {
1131 syslog(LOG_ERR, "Could not determine remote IP address");
1132 ipcp_close(f->unit, "Could not determine remote IP address");
1135 if (go->ouraddr == 0) {
1136 syslog(LOG_ERR, "Could not determine local IP address");
1137 ipcp_close(f->unit, "Could not determine local IP address");
1142 * Check that the peer is allowed to use the IP address it wants.
1144 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1145 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1146 ip_ntoa(ho->hisaddr));
1147 ipcp_close(f->unit, "Unauthorized remote IP address");
1151 /* set tcp compression */
1152 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1155 * If we are doing dial-on-demand, the interface is already
1156 * configured, so we put out any saved-up packets, then set the
1157 * interface to pass IP packets.
1160 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1161 syslog(LOG_ERR, "Failed to negotiate desired IP addresses");
1162 ipcp_close(f->unit, "Wrong IP addresses");
1165 demand_rexmit(PPP_IP);
1166 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1168 #ifndef _linux_ /* Linux destroys routes when the device goes down. */
1169 else /* always use the code which adds the routes. */
1173 * Set IP addresses and (if specified) netmask.
1175 mask = GetMask(go->ouraddr);
1176 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1177 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1178 ipcp_close(f->unit, "Interface configuration failed");
1182 /* bring the interface up for IP */
1183 if (!sifup(f->unit)) {
1184 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1185 ipcp_close(f->unit, "Interface configuration failed");
1188 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1190 /* assign a default route through the interface if required */
1191 if (ipcp_wantoptions[f->unit].default_route)
1192 if (sifdefaultroute(f->unit, ho->hisaddr))
1193 default_route_set[f->unit] = 1;
1195 /* Make a proxy ARP entry if requested. */
1196 if (ipcp_wantoptions[f->unit].proxy_arp)
1197 if (sifproxyarp(f->unit, ho->hisaddr))
1198 proxy_arp_set[f->unit] = 1;
1200 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1201 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1205 * Execute the ip-up script, like this:
1206 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1208 ipcp_script(f, _PATH_IPUP);
1214 * ipcp_down - IPCP has gone DOWN.
1216 * Take the IP network interface down, clear its addresses
1217 * and delete routes through it.
1223 u_int32_t ouraddr, hisaddr;
1225 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1226 np_down(f->unit, PPP_IP);
1227 sifvjcomp(f->unit, 0, 0, 0);
1230 * If we are doing dial-on-demand, set the interface
1231 * to queue up outgoing packets (for now).
1234 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1237 ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1238 hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1239 if (proxy_arp_set[f->unit]) {
1240 cifproxyarp(f->unit, hisaddr);
1241 proxy_arp_set[f->unit] = 0;
1243 if (default_route_set[f->unit]) {
1244 cifdefaultroute(f->unit, hisaddr);
1245 default_route_set[f->unit] = 0;
1248 cifaddr(f->unit, ouraddr, hisaddr);
1251 /* Execute the ip-down script */
1252 ipcp_script(f, _PATH_IPDOWN);
1257 * ipcp_finished - possibly shut down the lower layers.
1263 np_finished(f->unit, PPP_IP);
1268 * ipcp_script - Execute a script with arguments
1269 * interface-name tty-name speed local-IP remote-IP.
1272 ipcp_script(f, script)
1276 char strspeed[32], strlocal[32], strremote[32];
1279 sprintf(strspeed, "%d", baud_rate);
1280 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1281 strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1288 argv[5] = strremote;
1291 run_program(script, argv, 0);
1295 * ipcp_printpkt - print the contents of an IPCP packet.
1297 static char *ipcp_codenames[] = {
1298 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1299 "TermReq", "TermAck", "CodeRej"
1303 ipcp_printpkt(p, plen, printer, arg)
1306 void (*printer) __P((void *, char *, ...));
1309 int code, id, len, olen;
1310 u_char *pstart, *optend;
1314 if (plen < HEADERLEN)
1320 if (len < HEADERLEN || len > plen)
1323 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1324 printer(arg, " %s", ipcp_codenames[code-1]);
1326 printer(arg, " code=0x%x", code);
1327 printer(arg, " id=0x%x", id);
1334 /* print option list */
1339 if (olen < 2 || olen > len) {
1347 if (olen == CILEN_ADDRS) {
1350 printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1352 printer(arg, " %s", ip_ntoa(htonl(cilong)));
1355 case CI_COMPRESSTYPE:
1356 if (olen >= CILEN_COMPRESS) {
1358 GETSHORT(cishort, p);
1359 printer(arg, "compress ");
1364 case IPCP_VJ_COMP_OLD:
1365 printer(arg, "old-VJ");
1368 printer(arg, "0x%x", cishort);
1373 if (olen == CILEN_ADDR) {
1376 printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1380 while (p < optend) {
1382 printer(arg, " %.2x", code);
1390 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1392 print_string(p, len, printer, arg);
1399 /* print the rest of the bytes in the packet */
1400 for (; len > 0; --len) {
1402 printer(arg, " %.2x", code);
1409 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1410 * We don't bring the link up for IP fragments or for TCP FIN packets
1414 #define IP_OFFMASK 0x1fff
1418 ip_active_pkt(pkt, len)
1426 if (len < sizeof(struct ip) + PPP_HDRLEN)
1428 ip = (struct ip *) (pkt + PPP_HDRLEN);
1429 if ((ntohs(ip->ip_off) & IP_OFFMASK) != 0)
1431 if (ip->ip_p != IPPROTO_TCP)
1433 hlen = ip->ip_hl * 4;
1434 if (len < hlen + sizeof(struct tcphdr) + PPP_HDRLEN)
1436 tcp = (struct tcphdr *) (pkt + PPP_HDRLEN + hlen);
1437 if ((tcp->th_flags & TH_FIN) != 0 && hlen + tcp->th_off * 4 == len)