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.23 1996/01/01 22:56:53 paulus Exp $";
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
39 #include "pathnames.h"
42 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
43 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
44 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
45 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
48 static int cis_received[NUM_PPP]; /* # Conf-Reqs received */
49 static int default_route_set[NUM_PPP]; /* Have set up a default route */
50 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
53 * Callbacks for fsm code. (CI = Configuration Information)
55 static void ipcp_resetci __P((fsm *)); /* Reset our CI */
56 static int ipcp_cilen __P((fsm *)); /* Return length of our CI */
57 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
58 static int ipcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
59 static int ipcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
60 static int ipcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
61 static int ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
62 static void ipcp_up __P((fsm *)); /* We're UP */
63 static void ipcp_down __P((fsm *)); /* We're DOWN */
64 static void ipcp_script __P((fsm *, char *)); /* Run an up/down script */
65 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
67 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
69 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
70 ipcp_resetci, /* Reset our Configuration Information */
71 ipcp_cilen, /* Length of our Configuration Information */
72 ipcp_addci, /* Add our Configuration Information */
73 ipcp_ackci, /* ACK our Configuration Information */
74 ipcp_nakci, /* NAK our Configuration Information */
75 ipcp_rejci, /* Reject our Configuration Information */
76 ipcp_reqci, /* Request peer's Configuration Information */
77 ipcp_up, /* Called when fsm reaches OPENED state */
78 ipcp_down, /* Called when fsm leaves OPENED state */
79 NULL, /* Called when we want the lower layer up */
80 ipcp_finished, /* Called when we want the lower layer down */
81 NULL, /* Called when Protocol-Reject received */
82 NULL, /* Retransmission is necessary */
83 NULL, /* Called to handle protocol-specific codes */
84 "IPCP" /* String name of protocol */
87 struct protent ipcp_protent = {
88 PPP_IPCP, ipcp_init, ipcp_input, ipcp_protrej,
89 ipcp_lowerup, ipcp_lowerdown, ipcp_open, ipcp_close,
90 ipcp_printpkt, NULL, 1, "IPCP",
91 ip_check_options, ip_demand_conf,
95 * Lengths of configuration options.
98 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
99 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
100 #define CILEN_ADDR 6 /* new-style single address option */
101 #define CILEN_ADDRS 10 /* old-style dual address option */
104 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
105 (x) == CONFNAK ? "NAK" : "REJ")
109 * Make a string representation of a network IP address.
117 ipaddr = ntohl(ipaddr);
119 sprintf(b, "%d.%d.%d.%d",
120 (u_char)(ipaddr >> 24),
121 (u_char)(ipaddr >> 16),
122 (u_char)(ipaddr >> 8),
129 * ipcp_init - Initialize IPCP.
135 fsm *f = &ipcp_fsm[unit];
136 ipcp_options *wo = &ipcp_wantoptions[unit];
137 ipcp_options *ao = &ipcp_allowoptions[unit];
140 f->protocol = PPP_IPCP;
141 f->callbacks = &ipcp_callbacks;
142 fsm_init(&ipcp_fsm[unit]);
144 memset(wo, 0, sizeof(*wo));
145 memset(ao, 0, sizeof(*ao));
149 wo->vj_protocol = IPCP_VJ_COMP;
150 wo->maxslotindex = MAX_STATES - 1; /* really max index */
153 /* max slots and slot-id compression are currently hardwired in */
154 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
159 ao->maxslotindex = MAX_STATES - 1;
163 * XXX These control whether the user may use the proxyarp
164 * and defaultroute options.
167 ao->default_route = 1;
172 * ipcp_open - IPCP is allowed to come up.
178 fsm_open(&ipcp_fsm[unit]);
183 * ipcp_close - Take IPCP down.
186 ipcp_close(unit, reason)
190 fsm_close(&ipcp_fsm[unit], reason);
195 * ipcp_lowerup - The lower layer is up.
201 fsm_lowerup(&ipcp_fsm[unit]);
206 * ipcp_lowerdown - The lower layer is down.
212 fsm_lowerdown(&ipcp_fsm[unit]);
217 * ipcp_input - Input IPCP packet.
220 ipcp_input(unit, p, len)
225 fsm_input(&ipcp_fsm[unit], p, len);
230 * ipcp_protrej - A Protocol-Reject was received for IPCP.
232 * Pretend the lower layer went down, so we shut up.
238 fsm_lowerdown(&ipcp_fsm[unit]);
243 * ipcp_resetci - Reset our CI.
249 ipcp_options *wo = &ipcp_wantoptions[f->unit];
251 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
252 if (wo->ouraddr == 0)
253 wo->accept_local = 1;
254 if (wo->hisaddr == 0)
255 wo->accept_remote = 1;
256 ipcp_gotoptions[f->unit] = *wo;
257 cis_received[f->unit] = 0;
262 * ipcp_cilen - Return length of our CI.
268 ipcp_options *go = &ipcp_gotoptions[f->unit];
270 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
271 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
273 return (LENCIADDR(go->neg_addr, go->old_addrs) +
274 LENCIVJ(go->neg_vj, go->old_vj));
279 * ipcp_addci - Add our desired CIs to a packet.
282 ipcp_addci(f, ucp, lenp)
287 ipcp_options *wo = &ipcp_wantoptions[f->unit];
288 ipcp_options *go = &ipcp_gotoptions[f->unit];
289 ipcp_options *ho = &ipcp_hisoptions[f->unit];
292 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
294 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
295 if (len >= vjlen) { \
297 PUTCHAR(vjlen, ucp); \
298 PUTSHORT(val, ucp); \
300 PUTCHAR(maxslotindex, ucp); \
301 PUTCHAR(cflag, ucp); \
308 #define ADDCIADDR(opt, neg, old, val1, val2) \
310 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
311 if (len >= addrlen) { \
314 PUTCHAR(addrlen, ucp); \
327 * First see if we want to change our options to the old
328 * forms because we have received old forms from the peer.
330 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
331 /* use the old style of address negotiation */
335 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
336 /* try an older style of VJ negotiation */
337 if (cis_received[f->unit] == 0) {
338 /* keep trying the new style until we see some CI from the peer */
341 /* use the old style only if the peer did */
342 if (ho->neg_vj && ho->old_vj) {
345 go->vj_protocol = ho->vj_protocol;
350 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
351 go->old_addrs, go->ouraddr, go->hisaddr);
353 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
354 go->maxslotindex, go->cflag);
361 * ipcp_ackci - Ack our CIs.
368 ipcp_ackci(f, p, len)
373 ipcp_options *go = &ipcp_gotoptions[f->unit];
374 u_short cilen, citype, cishort;
376 u_char cimaxslotindex, cicflag;
379 * CIs must be in exactly the same order that we sent...
380 * Check packet length and CI length at each step.
381 * If we find any deviations, then this packet is bad.
384 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
386 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
387 if ((len -= vjlen) < 0) \
389 GETCHAR(citype, p); \
391 if (cilen != vjlen || \
394 GETSHORT(cishort, p); \
395 if (cishort != val) \
398 GETCHAR(cimaxslotindex, p); \
399 if (cimaxslotindex != maxslotindex) \
401 GETCHAR(cicflag, p); \
402 if (cicflag != cflag) \
407 #define ACKCIADDR(opt, neg, old, val1, val2) \
409 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
411 if ((len -= addrlen) < 0) \
413 GETCHAR(citype, p); \
415 if (cilen != addrlen || \
420 if (val1 != cilong) \
425 if (val2 != cilong) \
430 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
431 go->old_addrs, go->ouraddr, go->hisaddr);
433 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
434 go->maxslotindex, go->cflag);
437 * If there are any remaining CIs, then this packet is bad.
444 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
449 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
450 * This should not modify any state if the Nak is bad
451 * or if IPCP is in the OPENED state.
458 ipcp_nakci(f, p, len)
463 ipcp_options *go = &ipcp_gotoptions[f->unit];
464 u_char cimaxslotindex, cicflag;
465 u_char citype, cilen, *next;
467 u_int32_t ciaddr1, ciaddr2, l;
468 ipcp_options no; /* options we've seen Naks for */
469 ipcp_options try; /* options to request next time */
471 BZERO(&no, sizeof(no));
475 * Any Nak'd CIs must be in exactly the same order that we sent.
476 * Check packet length and CI length at each step.
477 * If we find any deviations, then this packet is bad.
479 #define NAKCIADDR(opt, neg, old, code) \
481 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
487 ciaddr1 = htonl(l); \
490 ciaddr2 = htonl(l); \
498 #define NAKCIVJ(opt, neg, code) \
500 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
505 GETSHORT(cishort, p); \
511 * Accept the peer's idea of {our,his} address, if different
512 * from our idea, only if the accept_{local,remote} flag is set.
514 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
515 if (go->accept_local && ciaddr1) { /* Do we know our address? */
516 try.ouraddr = ciaddr1;
517 IPCPDEBUG((LOG_INFO, "local IP address %s",
520 if (go->accept_remote && ciaddr2) { /* Does he know his? */
521 try.hisaddr = ciaddr2;
522 IPCPDEBUG((LOG_INFO, "remote IP address %s",
528 * Accept the peer's value of maxslotindex provided that it
529 * is less than what we asked for. Turn off slot-ID compression
530 * if the peer wants. Send old-style compress-type option if
533 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
534 if (cilen == CILEN_VJ) {
535 GETCHAR(cimaxslotindex, p);
537 if (cishort == IPCP_VJ_COMP) {
539 if (cimaxslotindex < go->maxslotindex)
540 try.maxslotindex = cimaxslotindex;
547 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
549 try.vj_protocol = cishort;
557 * There may be remaining CIs, if the peer is requesting negotiation
558 * on an option that we didn't include in our request packet.
559 * If they want to negotiate about IP addresses, we comply.
560 * If they want us to ask for compression, we refuse.
562 while (len > CILEN_VOID) {
565 if( (len -= cilen) < 0 )
567 next = p + cilen - 2;
570 case CI_COMPRESSTYPE:
571 if (go->neg_vj || no.neg_vj ||
572 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
577 if (go->neg_addr && go->old_addrs || no.old_addrs
578 || cilen != CILEN_ADDRS)
584 if (ciaddr1 && go->accept_local)
585 try.ouraddr = ciaddr1;
588 if (ciaddr2 && go->accept_remote)
589 try.hisaddr = ciaddr2;
593 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
598 if (ciaddr1 && go->accept_local)
599 try.ouraddr = ciaddr1;
600 if (try.ouraddr != 0)
608 /* If there is still anything left, this packet is bad. */
613 * OK, the Nak is good. Now we can update state.
615 if (f->state != OPENED)
621 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
627 * ipcp_rejci - Reject some of our CIs.
630 ipcp_rejci(f, p, len)
635 ipcp_options *go = &ipcp_gotoptions[f->unit];
636 u_char cimaxslotindex, ciflag, cilen;
639 ipcp_options try; /* options to request next time */
643 * Any Rejected CIs must be in exactly the same order that we sent.
644 * Check packet length and CI length at each step.
645 * If we find any deviations, then this packet is bad.
647 #define REJCIADDR(opt, neg, old, val1, val2) \
649 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
657 /* Check rejected value. */ \
658 if (cilong != val1) \
663 /* Check rejected value. */ \
664 if (cilong != val2) \
670 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
672 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
677 GETSHORT(cishort, p); \
678 /* Check rejected value. */ \
679 if (cishort != val) \
682 GETCHAR(cimaxslotindex, p); \
683 if (cimaxslotindex != maxslot) \
685 GETCHAR(ciflag, p); \
686 if (ciflag != cflag) \
692 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
693 go->old_addrs, go->ouraddr, go->hisaddr);
695 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
696 go->maxslotindex, go->cflag);
699 * If there are any remaining CIs, then this packet is bad.
704 * Now we can update state.
706 if (f->state != OPENED)
711 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
717 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
719 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
720 * appropriately. If reject_if_disagree is non-zero, doesn't return
721 * CONFNAK; returns CONFREJ if it can't return CONFACK.
724 ipcp_reqci(f, inp, len, reject_if_disagree)
726 u_char *inp; /* Requested CIs */
727 int *len; /* Length of requested CIs */
728 int reject_if_disagree;
730 ipcp_options *wo = &ipcp_wantoptions[f->unit];
731 ipcp_options *ho = &ipcp_hisoptions[f->unit];
732 ipcp_options *ao = &ipcp_allowoptions[f->unit];
733 ipcp_options *go = &ipcp_gotoptions[f->unit];
734 u_char *cip, *next; /* Pointer to current and next CIs */
735 u_short cilen, citype; /* Parsed len, type */
736 u_short cishort; /* Parsed short value */
737 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
738 int rc = CONFACK; /* Final packet return code */
739 int orc; /* Individual option return code */
740 u_char *p; /* Pointer to next char to parse */
741 u_char *ucp = inp; /* Pointer to current output char */
742 int l = *len; /* Length left */
743 u_char maxslotindex, cflag;
746 * Reset all his options.
748 BZERO(ho, sizeof(*ho));
751 * Process all his options.
755 orc = CONFACK; /* Assume success */
756 cip = p = next; /* Remember begining of CI */
757 if (l < 2 || /* Not enough data for CI header or */
758 p[1] < 2 || /* CI length too small or */
759 p[1] > l) { /* CI length too big? */
760 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
761 orc = CONFREJ; /* Reject bad CI */
762 cilen = l; /* Reject till end of packet */
763 l = 0; /* Don't loop again */
766 GETCHAR(citype, p); /* Parse CI type */
767 GETCHAR(cilen, p); /* Parse CI length */
768 l -= cilen; /* Adjust remaining length */
769 next += cilen; /* Step to next CI */
771 switch (citype) { /* Check CI type */
773 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
775 cilen != CILEN_ADDRS) { /* Check CI length */
776 orc = CONFREJ; /* Reject CI */
781 * If he has no address, or if we both have his address but
782 * disagree about it, then NAK it with our idea.
783 * In particular, if we don't know his address, but he does,
786 GETLONG(tl, p); /* Parse source address (his) */
788 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
789 if (ciaddr1 != wo->hisaddr
790 && (ciaddr1 == 0 || !wo->accept_remote)) {
792 if (!reject_if_disagree) {
793 DECPTR(sizeof(u_int32_t), p);
794 tl = ntohl(wo->hisaddr);
797 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
799 * If neither we nor he knows his address, reject the option.
802 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
807 * If he doesn't know our address, or if we both have our address
808 * but disagree about it, then NAK it with our idea.
810 GETLONG(tl, p); /* Parse desination address (ours) */
812 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
813 if (ciaddr2 != wo->ouraddr) {
814 if (ciaddr2 == 0 || !wo->accept_local) {
816 if (!reject_if_disagree) {
817 DECPTR(sizeof(u_int32_t), p);
818 tl = ntohl(wo->ouraddr);
822 go->ouraddr = ciaddr2; /* accept peer's idea */
828 ho->hisaddr = ciaddr1;
829 ho->ouraddr = ciaddr2;
833 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
836 cilen != CILEN_ADDR) { /* Check CI length */
837 orc = CONFREJ; /* Reject CI */
842 * If he has no address, or if we both have his address but
843 * disagree about it, then NAK it with our idea.
844 * In particular, if we don't know his address, but he does,
847 GETLONG(tl, p); /* Parse source address (his) */
849 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
850 if (ciaddr1 != wo->hisaddr
851 && (ciaddr1 == 0 || !wo->accept_remote)) {
853 if (!reject_if_disagree) {
854 DECPTR(sizeof(u_int32_t), p);
855 tl = ntohl(wo->hisaddr);
858 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
860 * Don't ACK an address of 0.0.0.0 - reject it instead.
863 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
868 ho->hisaddr = ciaddr1;
873 /* Microsoft primary DNS request */
874 IPCPDEBUG((LOG_INFO, "ipcp: received DNS1 Request "));
876 /* If we do not have a DNS address then we cannot send it */
877 if (ao->dnsaddr[0] == 0 ||
878 cilen != CILEN_ADDR) { /* Check CI length */
879 orc = CONFREJ; /* Reject CI */
883 if (htonl(tl) != ao->dnsaddr[0]) {
884 DECPTR(sizeof(u_int32_t), p);
885 tl = ntohl(ao->dnsaddr[0]);
892 /* Microsoft secondary DNS request */
893 IPCPDEBUG((LOG_INFO, "ipcp: received DNS2 Request "));
895 /* If we do not have a DNS address then we cannot send it */
896 if (ao->dnsaddr[1] == 0 || /* Yes, this is the first one! */
897 cilen != CILEN_ADDR) { /* Check CI length */
898 orc = CONFREJ; /* Reject CI */
902 if (htonl(tl) != ao->dnsaddr[1]) { /* and this is the 2nd one */
903 DECPTR(sizeof(u_int32_t), p);
904 tl = ntohl(ao->dnsaddr[1]);
911 case CI_COMPRESSTYPE:
912 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
914 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
918 GETSHORT(cishort, p);
919 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
921 if (!(cishort == IPCP_VJ_COMP ||
922 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
928 ho->vj_protocol = cishort;
929 if (cilen == CILEN_VJ) {
930 GETCHAR(maxslotindex, p);
931 if (maxslotindex > ao->maxslotindex) {
933 if (!reject_if_disagree){
935 PUTCHAR(ao->maxslotindex, p);
939 if (cflag && !ao->cflag) {
941 if (!reject_if_disagree){
943 PUTCHAR(wo->cflag, p);
946 ho->maxslotindex = maxslotindex;
950 ho->maxslotindex = MAX_STATES - 1;
961 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
963 if (orc == CONFACK && /* Good CI */
964 rc != CONFACK) /* but prior CI wasnt? */
965 continue; /* Don't send this one */
967 if (orc == CONFNAK) { /* Nak this CI? */
968 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
969 orc = CONFREJ; /* Get tough if so */
971 if (rc == CONFREJ) /* Rejecting prior CI? */
972 continue; /* Don't send this one */
973 if (rc == CONFACK) { /* Ack'd all prior CIs? */
974 rc = CONFNAK; /* Not anymore... */
975 ucp = inp; /* Backup */
980 if (orc == CONFREJ && /* Reject this CI */
981 rc != CONFREJ) { /* but no prior ones? */
983 ucp = inp; /* Backup */
986 /* Need to move CI? */
988 BCOPY(cip, ucp, cilen); /* Move it */
990 /* Update output pointer */
995 * If we aren't rejecting this packet, and we want to negotiate
996 * their address, and they didn't send their address, then we
997 * send a NAK with a CI_ADDR option appended. We assume the
998 * input buffer is long enough that we can append the extra
1001 if (rc != CONFREJ && !ho->neg_addr &&
1002 wo->req_addr && !reject_if_disagree) {
1003 if (rc == CONFACK) {
1005 ucp = inp; /* reset pointer */
1006 wo->req_addr = 0; /* don't ask again */
1008 PUTCHAR(CI_ADDR, ucp);
1009 PUTCHAR(CILEN_ADDR, ucp);
1010 tl = ntohl(wo->hisaddr);
1014 *len = ucp - inp; /* Compute output length */
1015 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1016 return (rc); /* Return final code */
1021 * ip_check_options - check that any IP-related options are OK,
1022 * and assign appropriate defaults.
1029 ipcp_options *wo = &ipcp_wantoptions[0];
1032 * Default our local IP address based on our hostname.
1033 * If local IP address already given, don't bother.
1035 if (wo->ouraddr == 0 && !disable_defaultip) {
1037 * Look up our hostname (possibly with domain name appended)
1038 * and take the first IP address as our local IP address.
1039 * If there isn't an IP address for our hostname, too bad.
1041 wo->accept_local = 1; /* don't insist on this default value */
1042 if ((hp = gethostbyname(hostname)) != NULL) {
1043 local = *(u_int32_t *)hp->h_addr;
1044 if (local != 0 && !bad_ip_adrs(local))
1045 wo->ouraddr = local;
1049 if (demand && wo->hisaddr == 0) {
1051 "Remote IP address must be specified for demand-dialling\n");
1058 * ip_demand_conf - configure the interface as though
1059 * IPCP were up, for use with dial-on-demand.
1065 ipcp_options *wo = &ipcp_wantoptions[u];
1067 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1069 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1073 if (wo->default_route)
1074 if (sifdefaultroute(u, wo->hisaddr))
1075 default_route_set[u] = 1;
1077 if (sifproxyarp(u, wo->hisaddr))
1078 proxy_arp_set[u] = 1;
1080 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1081 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1088 * ipcp_up - IPCP has come UP.
1090 * Configure the IP network interface appropriately and bring it up.
1097 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1098 ipcp_options *go = &ipcp_gotoptions[f->unit];
1099 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1101 np_up(f->unit, PPP_IP);
1102 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1105 * We must have a non-zero IP address for both ends of the link.
1108 ho->hisaddr = wo->hisaddr;
1110 if (ho->hisaddr == 0) {
1111 syslog(LOG_ERR, "Could not determine remote IP address");
1112 ipcp_close(f->unit, "Could not determine remote IP address");
1115 if (go->ouraddr == 0) {
1116 syslog(LOG_ERR, "Could not determine local IP address");
1117 ipcp_close(f->unit, "Could not determine local IP address");
1122 * Check that the peer is allowed to use the IP address it wants.
1124 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1125 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1126 ip_ntoa(ho->hisaddr));
1127 ipcp_close(f->unit, "Unauthorized remote IP address");
1131 /* set tcp compression */
1132 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1135 * If we are doing dial-on-demand, the interface is already
1136 * configured, so we put out any saved-up packets, then set the
1137 * interface to pass IP packets.
1140 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1141 syslog(LOG_ERR, "Failed to negotiate desired IP addresses");
1142 ipcp_close(f->unit, "Wrong IP addresses");
1145 demand_rexmit(PPP_IP);
1146 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1151 * Set IP addresses and (if specified) netmask.
1153 mask = GetMask(go->ouraddr);
1154 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1155 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1156 ipcp_close(f->unit, "Interface configuration failed");
1160 /* bring the interface up for IP */
1161 if (!sifup(f->unit)) {
1162 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1163 ipcp_close(f->unit, "Interface configuration failed");
1167 /* assign a default route through the interface if required */
1168 if (ipcp_wantoptions[f->unit].default_route)
1169 if (sifdefaultroute(f->unit, ho->hisaddr))
1170 default_route_set[f->unit] = 1;
1172 /* Make a proxy ARP entry if requested. */
1173 if (ipcp_wantoptions[f->unit].proxy_arp)
1174 if (sifproxyarp(f->unit, ho->hisaddr))
1175 proxy_arp_set[f->unit] = 1;
1177 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1178 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1182 * Execute the ip-up script, like this:
1183 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1185 ipcp_script(f, _PATH_IPUP);
1191 * ipcp_down - IPCP has gone DOWN.
1193 * Take the IP network interface down, clear its addresses
1194 * and delete routes through it.
1200 u_int32_t ouraddr, hisaddr;
1202 np_down(f->unit, PPP_IP);
1203 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1206 * If we are doing dial-on-demand, set the interface
1207 * to queue up outgoing packets (for now).
1210 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1213 ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1214 hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1215 if (proxy_arp_set[f->unit]) {
1216 cifproxyarp(f->unit, hisaddr);
1217 proxy_arp_set[f->unit] = 0;
1219 if (default_route_set[f->unit]) {
1220 cifdefaultroute(f->unit, hisaddr);
1221 default_route_set[f->unit] = 0;
1224 cifaddr(f->unit, ouraddr, hisaddr);
1227 /* Execute the ip-down script */
1228 ipcp_script(f, _PATH_IPDOWN);
1233 * ipcp_finished - possibly shut down the lower layers.
1239 np_finished(f->unit, PPP_IP);
1244 * ipcp_script - Execute a script with arguments
1245 * interface-name tty-name speed local-IP remote-IP.
1248 ipcp_script(f, script)
1252 char strspeed[32], strlocal[32], strremote[32];
1255 sprintf(strspeed, "%d", baud_rate);
1256 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1257 strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1264 argv[5] = strremote;
1267 run_program(script, argv, 0);
1271 * ipcp_printpkt - print the contents of an IPCP packet.
1273 char *ipcp_codenames[] = {
1274 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1275 "TermReq", "TermAck", "CodeRej"
1279 ipcp_printpkt(p, plen, printer, arg)
1285 int code, id, len, olen;
1286 u_char *pstart, *optend;
1290 if (plen < HEADERLEN)
1296 if (len < HEADERLEN || len > plen)
1299 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1300 printer(arg, " %s", ipcp_codenames[code-1]);
1302 printer(arg, " code=0x%x", code);
1303 printer(arg, " id=0x%x", id);
1310 /* print option list */
1315 if (olen < 2 || olen > len) {
1323 if (olen == CILEN_ADDRS) {
1326 printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1328 printer(arg, " %s", ip_ntoa(htonl(cilong)));
1331 case CI_COMPRESSTYPE:
1332 if (olen >= CILEN_COMPRESS) {
1334 GETSHORT(cishort, p);
1335 printer(arg, "compress ");
1340 case IPCP_VJ_COMP_OLD:
1341 printer(arg, "old-VJ");
1344 printer(arg, "0x%x", cishort);
1349 if (olen == CILEN_ADDR) {
1352 printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1356 while (p < optend) {
1358 printer(arg, " %.2x", code);
1365 /* print the rest of the bytes in the packet */
1366 for (; len > 0; --len) {
1368 printer(arg, " %.2x", code);