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.25 1996/05/28 00:40:47 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];
269 ipcp_options *wo = &ipcp_wantoptions[f->unit];
270 ipcp_options *ho = &ipcp_hisoptions[f->unit];
272 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
273 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
276 * First see if we want to change our options to the old
277 * forms because we have received old forms from the peer.
279 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
280 /* use the old style of address negotiation */
284 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
285 /* try an older style of VJ negotiation */
286 if (cis_received[f->unit] == 0) {
287 /* keep trying the new style until we see some CI from the peer */
290 /* use the old style only if the peer did */
291 if (ho->neg_vj && ho->old_vj) {
294 go->vj_protocol = ho->vj_protocol;
299 return (LENCIADDR(go->neg_addr, go->old_addrs) +
300 LENCIVJ(go->neg_vj, go->old_vj));
305 * ipcp_addci - Add our desired CIs to a packet.
308 ipcp_addci(f, ucp, lenp)
313 ipcp_options *go = &ipcp_gotoptions[f->unit];
316 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
318 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
319 if (len >= vjlen) { \
321 PUTCHAR(vjlen, ucp); \
322 PUTSHORT(val, ucp); \
324 PUTCHAR(maxslotindex, ucp); \
325 PUTCHAR(cflag, ucp); \
332 #define ADDCIADDR(opt, neg, old, val1, val2) \
334 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
335 if (len >= addrlen) { \
338 PUTCHAR(addrlen, ucp); \
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 cis_received[f->unit] = 1;
749 * Reset all his options.
751 BZERO(ho, sizeof(*ho));
754 * Process all his options.
758 orc = CONFACK; /* Assume success */
759 cip = p = next; /* Remember begining of CI */
760 if (l < 2 || /* Not enough data for CI header or */
761 p[1] < 2 || /* CI length too small or */
762 p[1] > l) { /* CI length too big? */
763 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
764 orc = CONFREJ; /* Reject bad CI */
765 cilen = l; /* Reject till end of packet */
766 l = 0; /* Don't loop again */
769 GETCHAR(citype, p); /* Parse CI type */
770 GETCHAR(cilen, p); /* Parse CI length */
771 l -= cilen; /* Adjust remaining length */
772 next += cilen; /* Step to next CI */
774 switch (citype) { /* Check CI type */
776 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
778 cilen != CILEN_ADDRS) { /* Check CI length */
779 orc = CONFREJ; /* Reject CI */
784 * If he has no address, or if we both have his address but
785 * disagree about it, then NAK it with our idea.
786 * In particular, if we don't know his address, but he does,
789 GETLONG(tl, p); /* Parse source address (his) */
791 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
792 if (ciaddr1 != wo->hisaddr
793 && (ciaddr1 == 0 || !wo->accept_remote)) {
795 if (!reject_if_disagree) {
796 DECPTR(sizeof(u_int32_t), p);
797 tl = ntohl(wo->hisaddr);
800 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
802 * If neither we nor he knows his address, reject the option.
805 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
810 * If he doesn't know our address, or if we both have our address
811 * but disagree about it, then NAK it with our idea.
813 GETLONG(tl, p); /* Parse desination address (ours) */
815 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
816 if (ciaddr2 != wo->ouraddr) {
817 if (ciaddr2 == 0 || !wo->accept_local) {
819 if (!reject_if_disagree) {
820 DECPTR(sizeof(u_int32_t), p);
821 tl = ntohl(wo->ouraddr);
825 go->ouraddr = ciaddr2; /* accept peer's idea */
831 ho->hisaddr = ciaddr1;
832 ho->ouraddr = ciaddr2;
836 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
839 cilen != CILEN_ADDR) { /* Check CI length */
840 orc = CONFREJ; /* Reject CI */
845 * If he has no address, or if we both have his address but
846 * disagree about it, then NAK it with our idea.
847 * In particular, if we don't know his address, but he does,
850 GETLONG(tl, p); /* Parse source address (his) */
852 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
853 if (ciaddr1 != wo->hisaddr
854 && (ciaddr1 == 0 || !wo->accept_remote)) {
856 if (!reject_if_disagree) {
857 DECPTR(sizeof(u_int32_t), p);
858 tl = ntohl(wo->hisaddr);
861 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
863 * Don't ACK an address of 0.0.0.0 - reject it instead.
866 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
871 ho->hisaddr = ciaddr1;
876 /* Microsoft primary or secondary DNS request */
877 d = citype == CI_MS_DNS2;
878 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
880 /* If we do not have a DNS address then we cannot send it */
881 if (ao->dnsaddr[d] == 0 ||
882 cilen != CILEN_ADDR) { /* Check CI length */
883 orc = CONFREJ; /* Reject CI */
887 if (htonl(tl) != ao->dnsaddr[d]) {
888 DECPTR(sizeof(u_int32_t), p);
889 tl = ntohl(ao->dnsaddr[d]);
895 case CI_COMPRESSTYPE:
896 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
898 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
902 GETSHORT(cishort, p);
903 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
905 if (!(cishort == IPCP_VJ_COMP ||
906 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
912 ho->vj_protocol = cishort;
913 if (cilen == CILEN_VJ) {
914 GETCHAR(maxslotindex, p);
915 if (maxslotindex > ao->maxslotindex) {
917 if (!reject_if_disagree){
919 PUTCHAR(ao->maxslotindex, p);
923 if (cflag && !ao->cflag) {
925 if (!reject_if_disagree){
927 PUTCHAR(wo->cflag, p);
930 ho->maxslotindex = maxslotindex;
934 ho->maxslotindex = MAX_STATES - 1;
945 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
947 if (orc == CONFACK && /* Good CI */
948 rc != CONFACK) /* but prior CI wasnt? */
949 continue; /* Don't send this one */
951 if (orc == CONFNAK) { /* Nak this CI? */
952 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
953 orc = CONFREJ; /* Get tough if so */
955 if (rc == CONFREJ) /* Rejecting prior CI? */
956 continue; /* Don't send this one */
957 if (rc == CONFACK) { /* Ack'd all prior CIs? */
958 rc = CONFNAK; /* Not anymore... */
959 ucp = inp; /* Backup */
964 if (orc == CONFREJ && /* Reject this CI */
965 rc != CONFREJ) { /* but no prior ones? */
967 ucp = inp; /* Backup */
970 /* Need to move CI? */
972 BCOPY(cip, ucp, cilen); /* Move it */
974 /* Update output pointer */
979 * If we aren't rejecting this packet, and we want to negotiate
980 * their address, and they didn't send their address, then we
981 * send a NAK with a CI_ADDR option appended. We assume the
982 * input buffer is long enough that we can append the extra
985 if (rc != CONFREJ && !ho->neg_addr &&
986 wo->req_addr && !reject_if_disagree) {
989 ucp = inp; /* reset pointer */
990 wo->req_addr = 0; /* don't ask again */
992 PUTCHAR(CI_ADDR, ucp);
993 PUTCHAR(CILEN_ADDR, ucp);
994 tl = ntohl(wo->hisaddr);
998 *len = ucp - inp; /* Compute output length */
999 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1000 return (rc); /* Return final code */
1005 * ip_check_options - check that any IP-related options are OK,
1006 * and assign appropriate defaults.
1013 ipcp_options *wo = &ipcp_wantoptions[0];
1016 * Default our local IP address based on our hostname.
1017 * If local IP address already given, don't bother.
1019 if (wo->ouraddr == 0 && !disable_defaultip) {
1021 * Look up our hostname (possibly with domain name appended)
1022 * and take the first IP address as our local IP address.
1023 * If there isn't an IP address for our hostname, too bad.
1025 wo->accept_local = 1; /* don't insist on this default value */
1026 if ((hp = gethostbyname(hostname)) != NULL) {
1027 local = *(u_int32_t *)hp->h_addr;
1028 if (local != 0 && !bad_ip_adrs(local))
1029 wo->ouraddr = local;
1033 if (demand && wo->hisaddr == 0) {
1034 fprintf(stderr, "%s: remote IP address required for demand-dialling\n",
1038 if (demand && wo->accept_remote) {
1039 fprintf(stderr, "%s: ipcp-accept-remote is incompatible with demand\n",
1047 * ip_demand_conf - configure the interface as though
1048 * IPCP were up, for use with dial-on-demand.
1054 ipcp_options *wo = &ipcp_wantoptions[u];
1056 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1058 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1062 if (wo->default_route)
1063 if (sifdefaultroute(u, wo->hisaddr))
1064 default_route_set[u] = 1;
1066 if (sifproxyarp(u, wo->hisaddr))
1067 proxy_arp_set[u] = 1;
1069 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1070 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1077 * ipcp_up - IPCP has come UP.
1079 * Configure the IP network interface appropriately and bring it up.
1086 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1087 ipcp_options *go = &ipcp_gotoptions[f->unit];
1088 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1090 np_up(f->unit, PPP_IP);
1091 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1094 * We must have a non-zero IP address for both ends of the link.
1097 ho->hisaddr = wo->hisaddr;
1099 if (ho->hisaddr == 0) {
1100 syslog(LOG_ERR, "Could not determine remote IP address");
1101 ipcp_close(f->unit, "Could not determine remote IP address");
1104 if (go->ouraddr == 0) {
1105 syslog(LOG_ERR, "Could not determine local IP address");
1106 ipcp_close(f->unit, "Could not determine local IP address");
1111 * Check that the peer is allowed to use the IP address it wants.
1113 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1114 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1115 ip_ntoa(ho->hisaddr));
1116 ipcp_close(f->unit, "Unauthorized remote IP address");
1120 /* set tcp compression */
1121 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1124 * If we are doing dial-on-demand, the interface is already
1125 * configured, so we put out any saved-up packets, then set the
1126 * interface to pass IP packets.
1129 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1130 syslog(LOG_ERR, "Failed to negotiate desired IP addresses");
1131 ipcp_close(f->unit, "Wrong IP addresses");
1134 demand_rexmit(PPP_IP);
1135 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1140 * Set IP addresses and (if specified) netmask.
1142 mask = GetMask(go->ouraddr);
1143 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1144 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1145 ipcp_close(f->unit, "Interface configuration failed");
1149 /* bring the interface up for IP */
1150 if (!sifup(f->unit)) {
1151 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1152 ipcp_close(f->unit, "Interface configuration failed");
1155 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1157 /* assign a default route through the interface if required */
1158 if (ipcp_wantoptions[f->unit].default_route)
1159 if (sifdefaultroute(f->unit, ho->hisaddr))
1160 default_route_set[f->unit] = 1;
1162 /* Make a proxy ARP entry if requested. */
1163 if (ipcp_wantoptions[f->unit].proxy_arp)
1164 if (sifproxyarp(f->unit, ho->hisaddr))
1165 proxy_arp_set[f->unit] = 1;
1167 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1168 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1172 * Execute the ip-up script, like this:
1173 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1175 ipcp_script(f, _PATH_IPUP);
1181 * ipcp_down - IPCP has gone DOWN.
1183 * Take the IP network interface down, clear its addresses
1184 * and delete routes through it.
1190 u_int32_t ouraddr, hisaddr;
1192 np_down(f->unit, PPP_IP);
1193 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1196 * If we are doing dial-on-demand, set the interface
1197 * to queue up outgoing packets (for now).
1200 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1203 ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1204 hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1205 if (proxy_arp_set[f->unit]) {
1206 cifproxyarp(f->unit, hisaddr);
1207 proxy_arp_set[f->unit] = 0;
1209 if (default_route_set[f->unit]) {
1210 cifdefaultroute(f->unit, hisaddr);
1211 default_route_set[f->unit] = 0;
1214 cifaddr(f->unit, ouraddr, hisaddr);
1217 /* Execute the ip-down script */
1218 ipcp_script(f, _PATH_IPDOWN);
1223 * ipcp_finished - possibly shut down the lower layers.
1229 np_finished(f->unit, PPP_IP);
1234 * ipcp_script - Execute a script with arguments
1235 * interface-name tty-name speed local-IP remote-IP.
1238 ipcp_script(f, script)
1242 char strspeed[32], strlocal[32], strremote[32];
1245 sprintf(strspeed, "%d", baud_rate);
1246 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1247 strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1254 argv[5] = strremote;
1257 run_program(script, argv, 0);
1261 * ipcp_printpkt - print the contents of an IPCP packet.
1263 char *ipcp_codenames[] = {
1264 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1265 "TermReq", "TermAck", "CodeRej"
1269 ipcp_printpkt(p, plen, printer, arg)
1275 int code, id, len, olen;
1276 u_char *pstart, *optend;
1280 if (plen < HEADERLEN)
1286 if (len < HEADERLEN || len > plen)
1289 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1290 printer(arg, " %s", ipcp_codenames[code-1]);
1292 printer(arg, " code=0x%x", code);
1293 printer(arg, " id=0x%x", id);
1300 /* print option list */
1305 if (olen < 2 || olen > len) {
1313 if (olen == CILEN_ADDRS) {
1316 printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1318 printer(arg, " %s", ip_ntoa(htonl(cilong)));
1321 case CI_COMPRESSTYPE:
1322 if (olen >= CILEN_COMPRESS) {
1324 GETSHORT(cishort, p);
1325 printer(arg, "compress ");
1330 case IPCP_VJ_COMP_OLD:
1331 printer(arg, "old-VJ");
1334 printer(arg, "0x%x", cishort);
1339 if (olen == CILEN_ADDR) {
1342 printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1346 while (p < optend) {
1348 printer(arg, " %.2x", code);
1355 /* print the rest of the bytes in the packet */
1356 for (; len > 0; --len) {
1358 printer(arg, " %.2x", code);