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.
19 * $FreeBSD: src/usr.sbin/pppd/ipcp.c,v 1.12 1999/08/28 01:19:03 peter Exp $
20 * $DragonFly: src/usr.sbin/pppd/ipcp.c,v 1.4 2005/11/24 23:42:54 swildner Exp $
31 #include <sys/param.h>
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(fsm *); /* Reset our CI */
56 static int ipcp_cilen(fsm *); /* Return length of our CI */
57 static void ipcp_addci(fsm *, u_char *, int *); /* Add our CI */
58 static int ipcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
59 static int ipcp_nakci(fsm *, u_char *, int); /* Peer nak'd our CI */
60 static int ipcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
61 static int ipcp_reqci(fsm *, u_char *, int *, int); /* Rcv CI */
62 static void ipcp_up(fsm *); /* We're UP */
63 static void ipcp_down(fsm *); /* We're DOWN */
64 static void ipcp_script(fsm *, char *); /* Run an up/down script */
65 static void ipcp_finished(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 */
88 * Protocol entry points from main code.
90 static void ipcp_init(int);
91 static void ipcp_open(int);
92 static void ipcp_close(int, char *);
93 static void ipcp_lowerup(int);
94 static void ipcp_lowerdown(int);
95 static void ipcp_input(int, u_char *, int);
96 static void ipcp_protrej(int);
97 static int ipcp_printpkt(u_char *, int,
98 void (*)(void *, char *, ...), void *);
99 static void ip_check_options(void);
100 static int ip_demand_conf(int);
101 static int ip_active_pkt(u_char *, int);
103 struct protent ipcp_protent = {
121 static void ipcp_clear_addrs(int);
124 * Lengths of configuration options.
127 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
128 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
129 #define CILEN_ADDR 6 /* new-style single address option */
130 #define CILEN_ADDRS 10 /* old-style dual address option */
133 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
134 (x) == CONFNAK ? "NAK" : "REJ")
138 * Make a string representation of a network IP address.
141 ip_ntoa(u_int32_t ipaddr)
145 ipaddr = ntohl(ipaddr);
147 sprintf(b, "%d.%d.%d.%d",
148 (u_char)(ipaddr >> 24),
149 (u_char)(ipaddr >> 16),
150 (u_char)(ipaddr >> 8),
157 * ipcp_init - Initialize IPCP.
162 fsm *f = &ipcp_fsm[unit];
163 ipcp_options *wo = &ipcp_wantoptions[unit];
164 ipcp_options *ao = &ipcp_allowoptions[unit];
167 f->protocol = PPP_IPCP;
168 f->callbacks = &ipcp_callbacks;
169 fsm_init(&ipcp_fsm[unit]);
171 memset(wo, 0, sizeof(*wo));
172 memset(ao, 0, sizeof(*ao));
176 wo->vj_protocol = IPCP_VJ_COMP;
177 wo->maxslotindex = MAX_STATES - 1; /* really max index */
180 /* max slots and slot-id compression are currently hardwired in */
181 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
186 ao->maxslotindex = MAX_STATES - 1;
190 * XXX These control whether the user may use the proxyarp
191 * and defaultroute options.
194 ao->default_route = 1;
199 * ipcp_open - IPCP is allowed to come up.
204 fsm_open(&ipcp_fsm[unit]);
209 * ipcp_close - Take IPCP down.
212 ipcp_close(int unit, char *reason)
214 fsm_close(&ipcp_fsm[unit], reason);
219 * ipcp_lowerup - The lower layer is up.
222 ipcp_lowerup(int unit)
224 fsm_lowerup(&ipcp_fsm[unit]);
229 * ipcp_lowerdown - The lower layer is down.
232 ipcp_lowerdown(int unit)
234 fsm_lowerdown(&ipcp_fsm[unit]);
239 * ipcp_input - Input IPCP packet.
242 ipcp_input(int unit, u_char *p, int len)
244 fsm_input(&ipcp_fsm[unit], p, len);
249 * ipcp_protrej - A Protocol-Reject was received for IPCP.
251 * Pretend the lower layer went down, so we shut up.
254 ipcp_protrej(int unit)
256 fsm_lowerdown(&ipcp_fsm[unit]);
261 * ipcp_resetci - Reset our CI.
266 ipcp_options *wo = &ipcp_wantoptions[f->unit];
268 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
269 if (wo->ouraddr == 0)
270 wo->accept_local = 1;
271 if (wo->hisaddr == 0)
272 wo->accept_remote = 1;
273 ipcp_gotoptions[f->unit] = *wo;
274 cis_received[f->unit] = 0;
279 * ipcp_cilen - Return length of our CI.
284 ipcp_options *go = &ipcp_gotoptions[f->unit];
285 ipcp_options *wo = &ipcp_wantoptions[f->unit];
286 ipcp_options *ho = &ipcp_hisoptions[f->unit];
288 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
289 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
292 * First see if we want to change our options to the old
293 * forms because we have received old forms from the peer.
295 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
296 /* use the old style of address negotiation */
300 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
301 /* try an older style of VJ negotiation */
302 if (cis_received[f->unit] == 0) {
303 /* keep trying the new style until we see some CI from the peer */
306 /* use the old style only if the peer did */
307 if (ho->neg_vj && ho->old_vj) {
310 go->vj_protocol = ho->vj_protocol;
315 return (LENCIADDR(go->neg_addr, go->old_addrs) +
316 LENCIVJ(go->neg_vj, go->old_vj));
321 * ipcp_addci - Add our desired CIs to a packet.
324 ipcp_addci(fsm *f, u_char *ucp, int *lenp)
326 ipcp_options *go = &ipcp_gotoptions[f->unit];
329 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
331 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
332 if (len >= vjlen) { \
334 PUTCHAR(vjlen, ucp); \
335 PUTSHORT(val, ucp); \
337 PUTCHAR(maxslotindex, ucp); \
338 PUTCHAR(cflag, ucp); \
345 #define ADDCIADDR(opt, neg, old, val1, val2) \
347 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
348 if (len >= addrlen) { \
351 PUTCHAR(addrlen, ucp); \
363 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
364 go->old_addrs, go->ouraddr, go->hisaddr);
366 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
367 go->maxslotindex, go->cflag);
374 * ipcp_ackci - Ack our CIs.
381 ipcp_ackci(fsm *f, u_char *p, int len)
383 ipcp_options *go = &ipcp_gotoptions[f->unit];
384 u_short cilen, citype, cishort;
386 u_char cimaxslotindex, cicflag;
389 * CIs must be in exactly the same order that we sent...
390 * Check packet length and CI length at each step.
391 * If we find any deviations, then this packet is bad.
394 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
396 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
397 if ((len -= vjlen) < 0) \
399 GETCHAR(citype, p); \
401 if (cilen != vjlen || \
404 GETSHORT(cishort, p); \
405 if (cishort != val) \
408 GETCHAR(cimaxslotindex, p); \
409 if (cimaxslotindex != maxslotindex) \
411 GETCHAR(cicflag, p); \
412 if (cicflag != cflag) \
417 #define ACKCIADDR(opt, neg, old, val1, val2) \
419 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
421 if ((len -= addrlen) < 0) \
423 GETCHAR(citype, p); \
425 if (cilen != addrlen || \
430 if (val1 != cilong) \
435 if (val2 != cilong) \
440 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
441 go->old_addrs, go->ouraddr, go->hisaddr);
443 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
444 go->maxslotindex, go->cflag);
447 * If there are any remaining CIs, then this packet is bad.
454 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
459 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
460 * This should not modify any state if the Nak is bad
461 * or if IPCP is in the OPENED state.
468 ipcp_nakci(fsm *f, u_char *p, int len)
470 ipcp_options *go = &ipcp_gotoptions[f->unit];
471 u_char cimaxslotindex, cicflag;
472 u_char citype, cilen, *next;
474 u_int32_t ciaddr1, ciaddr2, l;
475 ipcp_options no; /* options we've seen Naks for */
476 ipcp_options try; /* options to request next time */
478 BZERO(&no, sizeof(no));
482 * Any Nak'd CIs must be in exactly the same order that we sent.
483 * Check packet length and CI length at each step.
484 * If we find any deviations, then this packet is bad.
486 #define NAKCIADDR(opt, neg, old, code) \
488 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
494 ciaddr1 = htonl(l); \
497 ciaddr2 = htonl(l); \
505 #define NAKCIVJ(opt, neg, code) \
507 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
512 GETSHORT(cishort, p); \
518 * Accept the peer's idea of {our,his} address, if different
519 * from our idea, only if the accept_{local,remote} flag is set.
521 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
522 if (go->accept_local && ciaddr1) { /* Do we know our address? */
523 try.ouraddr = ciaddr1;
524 IPCPDEBUG((LOG_INFO, "local IP address %s",
527 if (go->accept_remote && ciaddr2) { /* Does he know his? */
528 try.hisaddr = ciaddr2;
529 IPCPDEBUG((LOG_INFO, "remote IP address %s",
535 * Accept the peer's value of maxslotindex provided that it
536 * is less than what we asked for. Turn off slot-ID compression
537 * if the peer wants. Send old-style compress-type option if
540 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
541 if (cilen == CILEN_VJ) {
542 GETCHAR(cimaxslotindex, p);
544 if (cishort == IPCP_VJ_COMP) {
546 if (cimaxslotindex < go->maxslotindex)
547 try.maxslotindex = cimaxslotindex;
554 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
556 try.vj_protocol = cishort;
564 * There may be remaining CIs, if the peer is requesting negotiation
565 * on an option that we didn't include in our request packet.
566 * If they want to negotiate about IP addresses, we comply.
567 * If they want us to ask for compression, we refuse.
569 while (len > CILEN_VOID) {
572 if( (len -= cilen) < 0 )
574 next = p + cilen - 2;
577 case CI_COMPRESSTYPE:
578 if (go->neg_vj || no.neg_vj ||
579 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
584 if ((go->neg_addr && go->old_addrs) || no.old_addrs
585 || cilen != CILEN_ADDRS)
591 if (ciaddr1 && go->accept_local)
592 try.ouraddr = ciaddr1;
595 if (ciaddr2 && go->accept_remote)
596 try.hisaddr = ciaddr2;
600 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
605 if (ciaddr1 && go->accept_local)
606 try.ouraddr = ciaddr1;
607 if (try.ouraddr != 0)
615 /* If there is still anything left, this packet is bad. */
620 * OK, the Nak is good. Now we can update state.
622 if (f->state != OPENED)
628 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
634 * ipcp_rejci - Reject some of our CIs.
637 ipcp_rejci(fsm *f, u_char *p, int len)
639 ipcp_options *go = &ipcp_gotoptions[f->unit];
640 u_char cimaxslotindex, ciflag, cilen;
643 ipcp_options try; /* options to request next time */
647 * Any Rejected CIs must be in exactly the same order that we sent.
648 * Check packet length and CI length at each step.
649 * If we find any deviations, then this packet is bad.
651 #define REJCIADDR(opt, neg, old, val1, val2) \
653 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
661 /* Check rejected value. */ \
662 if (cilong != val1) \
667 /* Check rejected value. */ \
668 if (cilong != val2) \
674 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
676 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
681 GETSHORT(cishort, p); \
682 /* Check rejected value. */ \
683 if (cishort != val) \
686 GETCHAR(cimaxslotindex, p); \
687 if (cimaxslotindex != maxslot) \
689 GETCHAR(ciflag, p); \
690 if (ciflag != cflag) \
696 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
697 go->old_addrs, go->ouraddr, go->hisaddr);
699 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
700 go->maxslotindex, go->cflag);
703 * If there are any remaining CIs, then this packet is bad.
708 * Now we can update state.
710 if (f->state != OPENED)
715 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
721 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
723 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
724 * appropriately. If reject_if_disagree is non-zero, doesn't return
725 * CONFNAK; returns CONFREJ if it can't return CONFACK.
729 * len: Length of requested CIs
732 ipcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
734 ipcp_options *wo = &ipcp_wantoptions[f->unit];
735 ipcp_options *ho = &ipcp_hisoptions[f->unit];
736 ipcp_options *ao = &ipcp_allowoptions[f->unit];
737 ipcp_options *go = &ipcp_gotoptions[f->unit];
738 u_char *cip, *next; /* Pointer to current and next CIs */
739 u_short cilen, citype; /* Parsed len, type */
740 u_short cishort; /* Parsed short value */
741 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
742 int rc = CONFACK; /* Final packet return code */
743 int orc; /* Individual option return code */
744 u_char *p; /* Pointer to next char to parse */
745 u_char *ucp = inp; /* Pointer to current output char */
746 int l = *len; /* Length left */
747 u_char maxslotindex, cflag;
750 cis_received[f->unit] = 1;
753 * Reset all his options.
755 BZERO(ho, sizeof(*ho));
758 * Process all his options.
762 orc = CONFACK; /* Assume success */
763 cip = p = next; /* Remember begining of CI */
764 if (l < 2 || /* Not enough data for CI header or */
765 p[1] < 2 || /* CI length too small or */
766 p[1] > l) { /* CI length too big? */
767 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
768 orc = CONFREJ; /* Reject bad CI */
769 cilen = l; /* Reject till end of packet */
770 l = 0; /* Don't loop again */
773 GETCHAR(citype, p); /* Parse CI type */
774 GETCHAR(cilen, p); /* Parse CI length */
775 l -= cilen; /* Adjust remaining length */
776 next += cilen; /* Step to next CI */
778 switch (citype) { /* Check CI type */
780 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
782 cilen != CILEN_ADDRS) { /* Check CI length */
783 orc = CONFREJ; /* Reject CI */
788 * If he has no address, or if we both have his address but
789 * disagree about it, then NAK it with our idea.
790 * In particular, if we don't know his address, but he does,
793 GETLONG(tl, p); /* Parse source address (his) */
795 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
796 if (ciaddr1 != wo->hisaddr
797 && (ciaddr1 == 0 || !wo->accept_remote)) {
799 if (!reject_if_disagree) {
800 DECPTR(sizeof(u_int32_t), p);
801 tl = ntohl(wo->hisaddr);
804 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
806 * If neither we nor he knows his address, reject the option.
809 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
814 * If he doesn't know our address, or if we both have our address
815 * but disagree about it, then NAK it with our idea.
817 GETLONG(tl, p); /* Parse desination address (ours) */
819 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
820 if (ciaddr2 != wo->ouraddr) {
821 if (ciaddr2 == 0 || !wo->accept_local) {
823 if (!reject_if_disagree) {
824 DECPTR(sizeof(u_int32_t), p);
825 tl = ntohl(wo->ouraddr);
829 go->ouraddr = ciaddr2; /* accept peer's idea */
835 ho->hisaddr = ciaddr1;
836 ho->ouraddr = ciaddr2;
840 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
843 cilen != CILEN_ADDR) { /* Check CI length */
844 orc = CONFREJ; /* Reject CI */
849 * If he has no address, or if we both have his address but
850 * disagree about it, then NAK it with our idea.
851 * In particular, if we don't know his address, but he does,
854 GETLONG(tl, p); /* Parse source address (his) */
856 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
857 if (ciaddr1 != wo->hisaddr
858 && (ciaddr1 == 0 || !wo->accept_remote)) {
860 if (!reject_if_disagree) {
861 DECPTR(sizeof(u_int32_t), p);
862 tl = ntohl(wo->hisaddr);
865 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
867 * Don't ACK an address of 0.0.0.0 - reject it instead.
870 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
875 ho->hisaddr = ciaddr1;
880 /* Microsoft primary or secondary DNS request */
881 d = citype == CI_MS_DNS2;
882 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
884 /* If we do not have a DNS address then we cannot send it */
885 if (ao->dnsaddr[d] == 0 ||
886 cilen != CILEN_ADDR) { /* Check CI length */
887 orc = CONFREJ; /* Reject CI */
891 if (htonl(tl) != ao->dnsaddr[d]) {
892 DECPTR(sizeof(u_int32_t), p);
893 tl = ntohl(ao->dnsaddr[d]);
901 /* Microsoft primary or secondary WINS request */
902 d = citype == CI_MS_WINS2;
903 IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
905 /* If we do not have a DNS address then we cannot send it */
906 if (ao->winsaddr[d] == 0 ||
907 cilen != CILEN_ADDR) { /* Check CI length */
908 orc = CONFREJ; /* Reject CI */
912 if (htonl(tl) != ao->winsaddr[d]) {
913 DECPTR(sizeof(u_int32_t), p);
914 tl = ntohl(ao->winsaddr[d]);
920 case CI_COMPRESSTYPE:
921 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
923 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
927 GETSHORT(cishort, p);
928 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
930 if (!(cishort == IPCP_VJ_COMP ||
931 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
937 ho->vj_protocol = cishort;
938 if (cilen == CILEN_VJ) {
939 GETCHAR(maxslotindex, p);
940 if (maxslotindex > ao->maxslotindex) {
942 if (!reject_if_disagree){
944 PUTCHAR(ao->maxslotindex, p);
948 if (cflag && !ao->cflag) {
950 if (!reject_if_disagree){
952 PUTCHAR(wo->cflag, p);
955 ho->maxslotindex = maxslotindex;
959 ho->maxslotindex = MAX_STATES - 1;
970 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
972 if (orc == CONFACK && /* Good CI */
973 rc != CONFACK) /* but prior CI wasnt? */
974 continue; /* Don't send this one */
976 if (orc == CONFNAK) { /* Nak this CI? */
977 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
978 orc = CONFREJ; /* Get tough if so */
980 if (rc == CONFREJ) /* Rejecting prior CI? */
981 continue; /* Don't send this one */
982 if (rc == CONFACK) { /* Ack'd all prior CIs? */
983 rc = CONFNAK; /* Not anymore... */
984 ucp = inp; /* Backup */
989 if (orc == CONFREJ && /* Reject this CI */
990 rc != CONFREJ) { /* but no prior ones? */
992 ucp = inp; /* Backup */
995 /* Need to move CI? */
997 BCOPY(cip, ucp, cilen); /* Move it */
999 /* Update output pointer */
1004 * If we aren't rejecting this packet, and we want to negotiate
1005 * their address, and they didn't send their address, then we
1006 * send a NAK with a CI_ADDR option appended. We assume the
1007 * input buffer is long enough that we can append the extra
1010 if (rc != CONFREJ && !ho->neg_addr &&
1011 wo->req_addr && !reject_if_disagree) {
1012 if (rc == CONFACK) {
1014 ucp = inp; /* reset pointer */
1015 wo->req_addr = 0; /* don't ask again */
1017 PUTCHAR(CI_ADDR, ucp);
1018 PUTCHAR(CILEN_ADDR, ucp);
1019 tl = ntohl(wo->hisaddr);
1023 *len = ucp - inp; /* Compute output length */
1024 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1025 return (rc); /* Return final code */
1030 * ip_check_options - check that any IP-related options are OK,
1031 * and assign appropriate defaults.
1034 ip_check_options(void)
1038 ipcp_options *wo = &ipcp_wantoptions[0];
1041 * Default our local IP address based on our hostname.
1042 * If local IP address already given, don't bother.
1044 if (wo->ouraddr == 0 && !disable_defaultip) {
1046 * Look up our hostname (possibly with domain name appended)
1047 * and take the first IP address as our local IP address.
1048 * If there isn't an IP address for our hostname, too bad.
1050 wo->accept_local = 1; /* don't insist on this default value */
1051 if ((hp = gethostbyname(hostname)) != NULL) {
1052 local = *(u_int32_t *)hp->h_addr;
1053 if (local != 0 && !bad_ip_adrs(local))
1054 wo->ouraddr = local;
1058 if (demand && wo->hisaddr == 0) {
1059 option_error("remote IP address required for demand-dialling\n");
1063 if (demand && wo->accept_remote) {
1064 option_error("ipcp-accept-remote is incompatible with demand\n");
1072 * ip_demand_conf - configure the interface as though
1073 * IPCP were up, for use with dial-on-demand.
1076 ip_demand_conf(int u)
1078 ipcp_options *wo = &ipcp_wantoptions[u];
1080 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1084 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1086 if (wo->default_route)
1087 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1088 default_route_set[u] = 1;
1090 if (sifproxyarp(u, wo->hisaddr))
1091 proxy_arp_set[u] = 1;
1093 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1094 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1101 * ipcp_up - IPCP has come UP.
1103 * Configure the IP network interface appropriately and bring it up.
1109 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1110 ipcp_options *go = &ipcp_gotoptions[f->unit];
1111 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1113 np_up(f->unit, PPP_IP);
1114 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1117 * We must have a non-zero IP address for both ends of the link.
1120 ho->hisaddr = wo->hisaddr;
1122 if (ho->hisaddr == 0) {
1123 syslog(LOG_ERR, "Could not determine remote IP address");
1124 ipcp_close(f->unit, "Could not determine remote IP address");
1127 if (go->ouraddr == 0) {
1128 syslog(LOG_ERR, "Could not determine local IP address");
1129 ipcp_close(f->unit, "Could not determine local IP address");
1132 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr));
1133 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr));
1136 * Check that the peer is allowed to use the IP address it wants.
1138 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1139 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1140 ip_ntoa(ho->hisaddr));
1141 ipcp_close(f->unit, "Unauthorized remote IP address");
1145 /* set tcp compression */
1146 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1149 * If we are doing dial-on-demand, the interface is already
1150 * configured, so we put out any saved-up packets, then set the
1151 * interface to pass IP packets.
1154 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1155 if (go->ouraddr != wo->ouraddr)
1156 syslog(LOG_WARNING, "Local IP address changed to %s",
1157 ip_ntoa(go->ouraddr));
1158 if (ho->hisaddr != wo->hisaddr)
1159 syslog(LOG_WARNING, "Remote IP address changed to %s",
1160 ip_ntoa(ho->hisaddr));
1161 ipcp_clear_addrs(f->unit);
1163 /* Set the interface to the new addresses */
1164 mask = GetMask(go->ouraddr);
1165 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1166 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1167 ipcp_close(f->unit, "Interface configuration failed");
1171 /* assign a default route through the interface if required */
1172 if (ipcp_wantoptions[f->unit].default_route)
1173 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1174 default_route_set[f->unit] = 1;
1176 /* Make a proxy ARP entry if requested. */
1177 if (ipcp_wantoptions[f->unit].proxy_arp)
1178 if (sifproxyarp(f->unit, ho->hisaddr))
1179 proxy_arp_set[f->unit] = 1;
1182 demand_rexmit(PPP_IP);
1183 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1187 * Set IP addresses and (if specified) netmask.
1189 mask = GetMask(go->ouraddr);
1191 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1192 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1193 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1194 ipcp_close(f->unit, "Interface configuration failed");
1199 /* bring the interface up for IP */
1200 if (!sifup(f->unit)) {
1201 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1202 ipcp_close(f->unit, "Interface configuration failed");
1206 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1207 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1208 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1209 ipcp_close(f->unit, "Interface configuration failed");
1213 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1215 /* assign a default route through the interface if required */
1216 if (ipcp_wantoptions[f->unit].default_route)
1217 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1218 default_route_set[f->unit] = 1;
1220 /* Make a proxy ARP entry if requested. */
1221 if (ipcp_wantoptions[f->unit].proxy_arp)
1222 if (sifproxyarp(f->unit, ho->hisaddr))
1223 proxy_arp_set[f->unit] = 1;
1225 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1226 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1230 * Execute the ip-up script, like this:
1231 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1233 ipcp_script(f, _PATH_IPUP);
1239 * ipcp_down - IPCP has gone DOWN.
1241 * Take the IP network interface down, clear its addresses
1242 * and delete routes through it.
1247 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1248 np_down(f->unit, PPP_IP);
1249 sifvjcomp(f->unit, 0, 0, 0);
1252 * If we are doing dial-on-demand, set the interface
1253 * to queue up outgoing packets (for now).
1256 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1259 ipcp_clear_addrs(f->unit);
1262 /* Execute the ip-down script */
1263 ipcp_script(f, _PATH_IPDOWN);
1268 * ipcp_clear_addrs() - clear the interface addresses, routes,
1269 * proxy arp entries, etc.
1272 ipcp_clear_addrs(int unit)
1274 u_int32_t ouraddr, hisaddr;
1276 ouraddr = ipcp_gotoptions[unit].ouraddr;
1277 hisaddr = ipcp_hisoptions[unit].hisaddr;
1278 if (proxy_arp_set[unit]) {
1279 cifproxyarp(unit, hisaddr);
1280 proxy_arp_set[unit] = 0;
1282 if (default_route_set[unit]) {
1283 cifdefaultroute(unit, ouraddr, hisaddr);
1284 default_route_set[unit] = 0;
1286 cifaddr(unit, ouraddr, hisaddr);
1291 * ipcp_finished - possibly shut down the lower layers.
1294 ipcp_finished(fsm *f)
1296 np_finished(f->unit, PPP_IP);
1301 * ipcp_script - Execute a script with arguments
1302 * interface-name tty-name speed local-IP remote-IP.
1305 ipcp_script(fsm *f, char *script)
1307 char strspeed[32], strlocal[32], strremote[32];
1310 sprintf(strspeed, "%d", baud_rate);
1311 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1312 strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1319 argv[5] = strremote;
1322 run_program(script, argv, 0);
1326 * ipcp_printpkt - print the contents of an IPCP packet.
1328 static char *ipcp_codenames[] = {
1329 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1330 "TermReq", "TermAck", "CodeRej"
1334 ipcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
1337 int code, id, len, olen;
1338 u_char *pstart, *optend;
1342 if (plen < HEADERLEN)
1348 if (len < HEADERLEN || len > plen)
1351 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1352 printer(arg, " %s", ipcp_codenames[code-1]);
1354 printer(arg, " code=0x%x", code);
1355 printer(arg, " id=0x%x", id);
1362 /* print option list */
1367 if (olen < 2 || olen > len) {
1375 if (olen == CILEN_ADDRS) {
1378 printer(arg, "addrs %I", htonl(cilong));
1380 printer(arg, " %I", htonl(cilong));
1383 case CI_COMPRESSTYPE:
1384 if (olen >= CILEN_COMPRESS) {
1386 GETSHORT(cishort, p);
1387 printer(arg, "compress ");
1392 case IPCP_VJ_COMP_OLD:
1393 printer(arg, "old-VJ");
1396 printer(arg, "0x%x", cishort);
1401 if (olen == CILEN_ADDR) {
1404 printer(arg, "addr %I", htonl(cilong));
1411 printer(arg, "ms-dns %I", htonl(cilong));
1417 printer(arg, "ms-wins %I", htonl(cilong));
1420 while (p < optend) {
1422 printer(arg, " %.2x", code);
1430 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1432 print_string(p, len, printer, arg);
1439 /* print the rest of the bytes in the packet */
1440 for (; len > 0; --len) {
1442 printer(arg, " %.2x", code);
1449 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1450 * We don't bring the link up for IP fragments or for TCP FIN packets
1453 #define IP_HDRLEN 20 /* bytes */
1454 #define IP_OFFMASK 0x1fff
1455 #define IPPROTO_TCP 6
1456 #define TCP_HDRLEN 20
1460 * We use these macros because the IP header may be at an odd address,
1461 * and some compilers might use word loads to get th_off or ip_hl.
1464 #define net_short(x) (((x)[0] << 8) + (x)[1])
1465 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1466 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1467 #define get_ipproto(x) (((unsigned char *)(x))[9])
1468 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1469 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1472 ip_active_pkt(u_char *pkt, int len)
1479 if (len < IP_HDRLEN)
1481 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1483 if (get_ipproto(pkt) != IPPROTO_TCP)
1485 hlen = get_iphl(pkt) * 4;
1486 if (len < hlen + TCP_HDRLEN)
1489 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)