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 const char rcsid[] = "$Id: ipcp.c,v 1.48 1999/08/12 04:25:20 paulus Exp $";
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
40 #include "pathnames.h"
43 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
44 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
45 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
46 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
48 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
51 static int default_route_set[NUM_PPP]; /* Have set up a default route */
52 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
53 static bool usepeerdns; /* Ask peer for DNS addrs */
54 static int ipcp_is_up; /* have called np_up() */
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_finished __P((fsm *)); /* Don't need lower layer */
70 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
72 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
73 ipcp_resetci, /* Reset our Configuration Information */
74 ipcp_cilen, /* Length of our Configuration Information */
75 ipcp_addci, /* Add our Configuration Information */
76 ipcp_ackci, /* ACK our Configuration Information */
77 ipcp_nakci, /* NAK our Configuration Information */
78 ipcp_rejci, /* Reject our Configuration Information */
79 ipcp_reqci, /* Request peer's Configuration Information */
80 ipcp_up, /* Called when fsm reaches OPENED state */
81 ipcp_down, /* Called when fsm leaves OPENED state */
82 NULL, /* Called when we want the lower layer up */
83 ipcp_finished, /* Called when we want the lower layer down */
84 NULL, /* Called when Protocol-Reject received */
85 NULL, /* Retransmission is necessary */
86 NULL, /* Called to handle protocol-specific codes */
87 "IPCP" /* String name of protocol */
91 * Command-line options.
93 static int setvjslots __P((char **));
94 static int setdnsaddr __P((char **));
95 static int setwinsaddr __P((char **));
97 static option_t ipcp_option_list[] = {
98 { "noip", o_bool, &ipcp_protent.enabled_flag,
99 "Disable IP and IPCP" },
100 { "-ip", o_bool, &ipcp_protent.enabled_flag,
101 "Disable IP and IPCP" },
102 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
103 "Disable VJ compression", OPT_A2COPY, &ipcp_allowoptions[0].neg_vj },
104 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
105 "Disable VJ compression", OPT_A2COPY, &ipcp_allowoptions[0].neg_vj },
106 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
107 "Disable VJ connection-ID compression", OPT_A2COPY,
108 &ipcp_allowoptions[0].cflag },
109 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
110 "Disable VJ connection-ID compression", OPT_A2COPY,
111 &ipcp_allowoptions[0].cflag },
112 { "vj-max-slots", 1, setvjslots,
113 "Set maximum VJ header slots" },
114 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
115 "Accept peer's address for us", 1 },
116 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
117 "Accept peer's address for it", 1 },
118 { "ipparam", o_string, &ipparam,
119 "Set ip script parameter" },
120 { "noipdefault", o_bool, &disable_defaultip,
121 "Don't use name for default IP adrs", 1 },
122 { "ms-dns", 1, setdnsaddr,
123 "DNS address for the peer's use" },
124 { "ms-wins", 1, setwinsaddr,
125 "Nameserver for SMB over TCP/IP for peer" },
126 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
127 "Set timeout for IPCP" },
128 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
129 "Set max #xmits for term-reqs" },
130 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
131 "Set max #xmits for conf-reqs" },
132 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
133 "Set max #conf-naks for IPCP" },
134 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
135 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
136 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
137 "disable defaultroute option", OPT_A2COPY,
138 &ipcp_wantoptions[0].default_route },
139 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
140 "disable defaultroute option", OPT_A2COPY,
141 &ipcp_wantoptions[0].default_route },
142 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
143 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
144 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
145 "disable proxyarp option", OPT_A2COPY,
146 &ipcp_wantoptions[0].proxy_arp },
147 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
148 "disable proxyarp option", OPT_A2COPY,
149 &ipcp_wantoptions[0].proxy_arp },
150 { "usepeerdns", o_bool, &usepeerdns,
151 "Ask peer for DNS address(es)", 1 },
156 * Protocol entry points from main code.
158 static void ipcp_init __P((int));
159 static void ipcp_open __P((int));
160 static void ipcp_close __P((int, char *));
161 static void ipcp_lowerup __P((int));
162 static void ipcp_lowerdown __P((int));
163 static void ipcp_input __P((int, u_char *, int));
164 static void ipcp_protrej __P((int));
165 static int ipcp_printpkt __P((u_char *, int,
166 void (*) __P((void *, char *, ...)), void *));
167 static void ip_check_options __P((void));
168 static int ip_demand_conf __P((int));
169 static int ip_active_pkt __P((u_char *, int));
170 static void create_resolv __P((u_int32_t, u_int32_t));
172 struct protent ipcp_protent = {
192 static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t));
193 static void ipcp_script __P((char *)); /* Run an up/down script */
194 static void ipcp_script_done __P((void *));
197 * Lengths of configuration options.
200 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
201 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
202 #define CILEN_ADDR 6 /* new-style single address option */
203 #define CILEN_ADDRS 10 /* old-style dual address option */
206 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
207 (x) == CONFNAK ? "NAK" : "REJ")
210 * This state variable is used to ensure that we don't
211 * run an ipcp-up/down script while one is already running.
213 static enum script_state {
217 static pid_t ipcp_script_pid;
220 * Make a string representation of a network IP address.
228 slprintf(b, sizeof(b), "%I", ipaddr);
237 * setvjslots - set maximum number of connection slots for VJ compression
245 if (!int_option(*argv, &value))
247 if (value < 2 || value > 16) {
248 option_error("vj-max-slots value must be between 2 and 16");
251 ipcp_wantoptions [0].maxslotindex =
252 ipcp_allowoptions[0].maxslotindex = value - 1;
257 * setdnsaddr - set the dns address(es)
266 dns = inet_addr(*argv);
268 if ((hp = gethostbyname(*argv)) == NULL) {
269 option_error("invalid address parameter '%s' for ms-dns option",
273 dns = *(u_int32_t *)hp->h_addr;
276 /* if there is no primary then update it. */
277 if (ipcp_allowoptions[0].dnsaddr[0] == 0)
278 ipcp_allowoptions[0].dnsaddr[0] = dns;
280 /* always set the secondary address value to the same value. */
281 ipcp_allowoptions[0].dnsaddr[1] = dns;
287 * setwinsaddr - set the wins address(es)
288 * This is primrarly used with the Samba package under UNIX or for pointing
289 * the caller to the existing WINS server on a Windows NT platform.
298 wins = inet_addr(*argv);
300 if ((hp = gethostbyname(*argv)) == NULL) {
301 option_error("invalid address parameter '%s' for ms-wins option",
305 wins = *(u_int32_t *)hp->h_addr;
308 /* if there is no primary then update it. */
309 if (ipcp_allowoptions[0].winsaddr[0] == 0)
310 ipcp_allowoptions[0].winsaddr[0] = wins;
312 /* always set the secondary address value to the same value. */
313 ipcp_allowoptions[0].winsaddr[1] = wins;
320 * ipcp_init - Initialize IPCP.
326 fsm *f = &ipcp_fsm[unit];
327 ipcp_options *wo = &ipcp_wantoptions[unit];
328 ipcp_options *ao = &ipcp_allowoptions[unit];
331 f->protocol = PPP_IPCP;
332 f->callbacks = &ipcp_callbacks;
333 fsm_init(&ipcp_fsm[unit]);
335 memset(wo, 0, sizeof(*wo));
336 memset(ao, 0, sizeof(*ao));
340 wo->vj_protocol = IPCP_VJ_COMP;
341 wo->maxslotindex = MAX_STATES - 1; /* really max index */
345 /* max slots and slot-id compression are currently hardwired in */
346 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
351 ao->maxslotindex = MAX_STATES - 1;
355 * XXX These control whether the user may use the proxyarp
356 * and defaultroute options.
359 ao->default_route = 1;
364 * ipcp_open - IPCP is allowed to come up.
370 fsm_open(&ipcp_fsm[unit]);
375 * ipcp_close - Take IPCP down.
378 ipcp_close(unit, reason)
382 fsm_close(&ipcp_fsm[unit], reason);
387 * ipcp_lowerup - The lower layer is up.
393 fsm_lowerup(&ipcp_fsm[unit]);
398 * ipcp_lowerdown - The lower layer is down.
404 fsm_lowerdown(&ipcp_fsm[unit]);
409 * ipcp_input - Input IPCP packet.
412 ipcp_input(unit, p, len)
417 fsm_input(&ipcp_fsm[unit], p, len);
422 * ipcp_protrej - A Protocol-Reject was received for IPCP.
424 * Pretend the lower layer went down, so we shut up.
430 fsm_lowerdown(&ipcp_fsm[unit]);
435 * ipcp_resetci - Reset our CI.
436 * Called by fsm_sconfreq, Send Configure Request.
442 ipcp_options *wo = &ipcp_wantoptions[f->unit];
443 ipcp_options *go = &ipcp_gotoptions[f->unit];
445 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
446 if (wo->ouraddr == 0 || disable_defaultip)
447 wo->accept_local = 1;
448 if (wo->hisaddr == 0)
449 wo->accept_remote = 1;
450 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
451 wo->req_dns2 = usepeerdns;
453 if (disable_defaultip)
459 * ipcp_cilen - Return length of our CI.
460 * Called by fsm_sconfreq, Send Configure Request.
466 ipcp_options *go = &ipcp_gotoptions[f->unit];
467 ipcp_options *wo = &ipcp_wantoptions[f->unit];
468 ipcp_options *ho = &ipcp_hisoptions[f->unit];
470 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
471 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
472 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
475 * First see if we want to change our options to the old
476 * forms because we have received old forms from the peer.
478 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
479 /* use the old style of address negotiation */
483 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
484 /* try an older style of VJ negotiation */
485 /* use the old style only if the peer did */
486 if (ho->neg_vj && ho->old_vj) {
489 go->vj_protocol = ho->vj_protocol;
493 return (LENCIADDR(go->neg_addr, go->old_addrs) +
494 LENCIVJ(go->neg_vj, go->old_vj) +
495 LENCIDNS(go->req_dns1) +
496 LENCIDNS(go->req_dns2)) ;
501 * ipcp_addci - Add our desired CIs to a packet.
502 * Called by fsm_sconfreq, Send Configure Request.
505 ipcp_addci(f, ucp, lenp)
510 ipcp_options *go = &ipcp_gotoptions[f->unit];
513 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
515 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
516 if (len >= vjlen) { \
518 PUTCHAR(vjlen, ucp); \
519 PUTSHORT(val, ucp); \
521 PUTCHAR(maxslotindex, ucp); \
522 PUTCHAR(cflag, ucp); \
529 #define ADDCIADDR(opt, neg, old, val1, val2) \
531 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
532 if (len >= addrlen) { \
535 PUTCHAR(addrlen, ucp); \
547 #define ADDCIDNS(opt, neg, addr) \
549 if (len >= CILEN_ADDR) { \
552 PUTCHAR(CILEN_ADDR, ucp); \
560 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
561 go->old_addrs, go->ouraddr, go->hisaddr);
563 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
564 go->maxslotindex, go->cflag);
566 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
568 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
575 * ipcp_ackci - Ack our CIs.
576 * Called by fsm_rconfack, Receive Configure ACK.
583 ipcp_ackci(f, p, len)
588 ipcp_options *go = &ipcp_gotoptions[f->unit];
589 u_short cilen, citype, cishort;
591 u_char cimaxslotindex, cicflag;
594 * CIs must be in exactly the same order that we sent...
595 * Check packet length and CI length at each step.
596 * If we find any deviations, then this packet is bad.
599 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
601 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
602 if ((len -= vjlen) < 0) \
604 GETCHAR(citype, p); \
606 if (cilen != vjlen || \
609 GETSHORT(cishort, p); \
610 if (cishort != val) \
613 GETCHAR(cimaxslotindex, p); \
614 if (cimaxslotindex != maxslotindex) \
616 GETCHAR(cicflag, p); \
617 if (cicflag != cflag) \
622 #define ACKCIADDR(opt, neg, old, val1, val2) \
624 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
626 if ((len -= addrlen) < 0) \
628 GETCHAR(citype, p); \
630 if (cilen != addrlen || \
635 if (val1 != cilong) \
640 if (val2 != cilong) \
645 #define ACKCIDNS(opt, neg, addr) \
648 if ((len -= CILEN_ADDR) < 0) \
650 GETCHAR(citype, p); \
652 if (cilen != CILEN_ADDR || citype != opt) \
656 if (addr != cilong) \
660 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
661 go->old_addrs, go->ouraddr, go->hisaddr);
663 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
664 go->maxslotindex, go->cflag);
666 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
668 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
671 * If there are any remaining CIs, then this packet is bad.
678 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
683 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
684 * This should not modify any state if the Nak is bad
685 * or if IPCP is in the OPENED state.
686 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
693 ipcp_nakci(f, p, len)
698 ipcp_options *go = &ipcp_gotoptions[f->unit];
699 u_char cimaxslotindex, cicflag;
700 u_char citype, cilen, *next;
702 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
703 ipcp_options no; /* options we've seen Naks for */
704 ipcp_options try; /* options to request next time */
706 BZERO(&no, sizeof(no));
710 * Any Nak'd CIs must be in exactly the same order that we sent.
711 * Check packet length and CI length at each step.
712 * If we find any deviations, then this packet is bad.
714 #define NAKCIADDR(opt, neg, old, code) \
716 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
722 ciaddr1 = htonl(l); \
725 ciaddr2 = htonl(l); \
733 #define NAKCIVJ(opt, neg, code) \
735 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
740 GETSHORT(cishort, p); \
745 #define NAKCIDNS(opt, neg, code) \
747 ((cilen = p[1]) == CILEN_ADDR) && \
753 cidnsaddr = htonl(l); \
759 * Accept the peer's idea of {our,his} address, if different
760 * from our idea, only if the accept_{local,remote} flag is set.
762 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
763 if (go->accept_local && ciaddr1) { /* Do we know our address? */
764 try.ouraddr = ciaddr1;
766 if (go->accept_remote && ciaddr2) { /* Does he know his? */
767 try.hisaddr = ciaddr2;
772 * Accept the peer's value of maxslotindex provided that it
773 * is less than what we asked for. Turn off slot-ID compression
774 * if the peer wants. Send old-style compress-type option if
777 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
778 if (cilen == CILEN_VJ) {
779 GETCHAR(cimaxslotindex, p);
781 if (cishort == IPCP_VJ_COMP) {
783 if (cimaxslotindex < go->maxslotindex)
784 try.maxslotindex = cimaxslotindex;
791 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
793 try.vj_protocol = cishort;
800 NAKCIDNS(CI_MS_DNS1, req_dns1,
801 try.dnsaddr[0] = cidnsaddr;
804 NAKCIDNS(CI_MS_DNS2, req_dns2,
805 try.dnsaddr[1] = cidnsaddr;
809 * There may be remaining CIs, if the peer is requesting negotiation
810 * on an option that we didn't include in our request packet.
811 * If they want to negotiate about IP addresses, we comply.
812 * If they want us to ask for compression, we refuse.
814 while (len > CILEN_VOID) {
817 if( (len -= cilen) < 0 )
819 next = p + cilen - 2;
822 case CI_COMPRESSTYPE:
823 if (go->neg_vj || no.neg_vj ||
824 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
829 if ((go->neg_addr && go->old_addrs) || no.old_addrs
830 || cilen != CILEN_ADDRS)
836 if (ciaddr1 && go->accept_local)
837 try.ouraddr = ciaddr1;
840 if (ciaddr2 && go->accept_remote)
841 try.hisaddr = ciaddr2;
845 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
850 if (ciaddr1 && go->accept_local)
851 try.ouraddr = ciaddr1;
852 if (try.ouraddr != 0)
861 * OK, the Nak is good. Now we can update state.
862 * If there are any remaining options, we ignore them.
864 if (f->state != OPENED)
870 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
876 * ipcp_rejci - Reject some of our CIs.
877 * Callback from fsm_rconfnakrej.
880 ipcp_rejci(f, p, len)
885 ipcp_options *go = &ipcp_gotoptions[f->unit];
886 u_char cimaxslotindex, ciflag, cilen;
889 ipcp_options try; /* options to request next time */
893 * Any Rejected CIs must be in exactly the same order that we sent.
894 * Check packet length and CI length at each step.
895 * If we find any deviations, then this packet is bad.
897 #define REJCIADDR(opt, neg, old, val1, val2) \
899 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
907 /* Check rejected value. */ \
908 if (cilong != val1) \
913 /* Check rejected value. */ \
914 if (cilong != val2) \
920 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
922 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
927 GETSHORT(cishort, p); \
928 /* Check rejected value. */ \
929 if (cishort != val) \
932 GETCHAR(cimaxslotindex, p); \
933 if (cimaxslotindex != maxslot) \
935 GETCHAR(ciflag, p); \
936 if (ciflag != cflag) \
942 #define REJCIDNS(opt, neg, dnsaddr) \
944 ((cilen = p[1]) == CILEN_ADDR) && \
952 /* Check rejected value. */ \
953 if (cilong != dnsaddr) \
959 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
960 go->old_addrs, go->ouraddr, go->hisaddr);
962 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
963 go->maxslotindex, go->cflag);
965 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
967 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
970 * If there are any remaining CIs, then this packet is bad.
975 * Now we can update state.
977 if (f->state != OPENED)
982 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
988 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
989 * Callback from fsm_rconfreq, Receive Configure Request
991 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
992 * appropriately. If reject_if_disagree is non-zero, doesn't return
993 * CONFNAK; returns CONFREJ if it can't return CONFACK.
996 ipcp_reqci(f, inp, len, reject_if_disagree)
998 u_char *inp; /* Requested CIs */
999 int *len; /* Length of requested CIs */
1000 int reject_if_disagree;
1002 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1003 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1004 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1005 ipcp_options *go = &ipcp_gotoptions[f->unit];
1006 u_char *cip, *next; /* Pointer to current and next CIs */
1007 u_short cilen, citype; /* Parsed len, type */
1008 u_short cishort; /* Parsed short value */
1009 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1010 int rc = CONFACK; /* Final packet return code */
1011 int orc; /* Individual option return code */
1012 u_char *p; /* Pointer to next char to parse */
1013 u_char *ucp = inp; /* Pointer to current output char */
1014 int l = *len; /* Length left */
1015 u_char maxslotindex, cflag;
1019 * Reset all his options.
1021 BZERO(ho, sizeof(*ho));
1024 * Process all his options.
1028 orc = CONFACK; /* Assume success */
1029 cip = p = next; /* Remember begining of CI */
1030 if (l < 2 || /* Not enough data for CI header or */
1031 p[1] < 2 || /* CI length too small or */
1032 p[1] > l) { /* CI length too big? */
1033 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1034 orc = CONFREJ; /* Reject bad CI */
1035 cilen = l; /* Reject till end of packet */
1036 l = 0; /* Don't loop again */
1039 GETCHAR(citype, p); /* Parse CI type */
1040 GETCHAR(cilen, p); /* Parse CI length */
1041 l -= cilen; /* Adjust remaining length */
1042 next += cilen; /* Step to next CI */
1044 switch (citype) { /* Check CI type */
1046 if (!ao->neg_addr ||
1047 cilen != CILEN_ADDRS) { /* Check CI length */
1048 orc = CONFREJ; /* Reject CI */
1053 * If he has no address, or if we both have his address but
1054 * disagree about it, then NAK it with our idea.
1055 * In particular, if we don't know his address, but he does,
1058 GETLONG(tl, p); /* Parse source address (his) */
1059 ciaddr1 = htonl(tl);
1060 if (ciaddr1 != wo->hisaddr
1061 && (ciaddr1 == 0 || !wo->accept_remote)) {
1063 if (!reject_if_disagree) {
1064 DECPTR(sizeof(u_int32_t), p);
1065 tl = ntohl(wo->hisaddr);
1068 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1070 * If neither we nor he knows his address, reject the option.
1073 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1078 * If he doesn't know our address, or if we both have our address
1079 * but disagree about it, then NAK it with our idea.
1081 GETLONG(tl, p); /* Parse desination address (ours) */
1082 ciaddr2 = htonl(tl);
1083 if (ciaddr2 != wo->ouraddr) {
1084 if (ciaddr2 == 0 || !wo->accept_local) {
1086 if (!reject_if_disagree) {
1087 DECPTR(sizeof(u_int32_t), p);
1088 tl = ntohl(wo->ouraddr);
1092 go->ouraddr = ciaddr2; /* accept peer's idea */
1098 ho->hisaddr = ciaddr1;
1099 ho->ouraddr = ciaddr2;
1103 if (!ao->neg_addr ||
1104 cilen != CILEN_ADDR) { /* Check CI length */
1105 orc = CONFREJ; /* Reject CI */
1110 * If he has no address, or if we both have his address but
1111 * disagree about it, then NAK it with our idea.
1112 * In particular, if we don't know his address, but he does,
1115 GETLONG(tl, p); /* Parse source address (his) */
1116 ciaddr1 = htonl(tl);
1117 if (ciaddr1 != wo->hisaddr
1118 && (ciaddr1 == 0 || !wo->accept_remote)) {
1120 if (!reject_if_disagree) {
1121 DECPTR(sizeof(u_int32_t), p);
1122 tl = ntohl(wo->hisaddr);
1125 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1127 * Don't ACK an address of 0.0.0.0 - reject it instead.
1130 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1135 ho->hisaddr = ciaddr1;
1140 /* Microsoft primary or secondary DNS request */
1141 d = citype == CI_MS_DNS2;
1143 /* If we do not have a DNS address then we cannot send it */
1144 if (ao->dnsaddr[d] == 0 ||
1145 cilen != CILEN_ADDR) { /* Check CI length */
1146 orc = CONFREJ; /* Reject CI */
1150 if (htonl(tl) != ao->dnsaddr[d]) {
1151 DECPTR(sizeof(u_int32_t), p);
1152 tl = ntohl(ao->dnsaddr[d]);
1160 /* Microsoft primary or secondary WINS request */
1161 d = citype == CI_MS_WINS2;
1163 /* If we do not have a DNS address then we cannot send it */
1164 if (ao->winsaddr[d] == 0 ||
1165 cilen != CILEN_ADDR) { /* Check CI length */
1166 orc = CONFREJ; /* Reject CI */
1170 if (htonl(tl) != ao->winsaddr[d]) {
1171 DECPTR(sizeof(u_int32_t), p);
1172 tl = ntohl(ao->winsaddr[d]);
1178 case CI_COMPRESSTYPE:
1180 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1184 GETSHORT(cishort, p);
1186 if (!(cishort == IPCP_VJ_COMP ||
1187 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1193 ho->vj_protocol = cishort;
1194 if (cilen == CILEN_VJ) {
1195 GETCHAR(maxslotindex, p);
1196 if (maxslotindex > ao->maxslotindex) {
1198 if (!reject_if_disagree){
1200 PUTCHAR(ao->maxslotindex, p);
1204 if (cflag && !ao->cflag) {
1206 if (!reject_if_disagree){
1208 PUTCHAR(wo->cflag, p);
1211 ho->maxslotindex = maxslotindex;
1215 ho->maxslotindex = MAX_STATES - 1;
1225 if (orc == CONFACK && /* Good CI */
1226 rc != CONFACK) /* but prior CI wasnt? */
1227 continue; /* Don't send this one */
1229 if (orc == CONFNAK) { /* Nak this CI? */
1230 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1231 orc = CONFREJ; /* Get tough if so */
1233 if (rc == CONFREJ) /* Rejecting prior CI? */
1234 continue; /* Don't send this one */
1235 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1236 rc = CONFNAK; /* Not anymore... */
1237 ucp = inp; /* Backup */
1242 if (orc == CONFREJ && /* Reject this CI */
1243 rc != CONFREJ) { /* but no prior ones? */
1245 ucp = inp; /* Backup */
1248 /* Need to move CI? */
1250 BCOPY(cip, ucp, cilen); /* Move it */
1252 /* Update output pointer */
1257 * If we aren't rejecting this packet, and we want to negotiate
1258 * their address, and they didn't send their address, then we
1259 * send a NAK with a CI_ADDR option appended. We assume the
1260 * input buffer is long enough that we can append the extra
1263 if (rc != CONFREJ && !ho->neg_addr &&
1264 wo->req_addr && !reject_if_disagree) {
1265 if (rc == CONFACK) {
1267 ucp = inp; /* reset pointer */
1268 wo->req_addr = 0; /* don't ask again */
1270 PUTCHAR(CI_ADDR, ucp);
1271 PUTCHAR(CILEN_ADDR, ucp);
1272 tl = ntohl(wo->hisaddr);
1276 *len = ucp - inp; /* Compute output length */
1277 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1278 return (rc); /* Return final code */
1283 * ip_check_options - check that any IP-related options are OK,
1284 * and assign appropriate defaults.
1291 ipcp_options *wo = &ipcp_wantoptions[0];
1294 * Default our local IP address based on our hostname.
1295 * If local IP address already given, don't bother.
1297 if (wo->ouraddr == 0) {
1299 * Look up our hostname (possibly with domain name appended)
1300 * and take the first IP address as our local IP address.
1301 * If there isn't an IP address for our hostname, too bad.
1303 wo->accept_local = 1; /* don't insist on this default value */
1304 if ((hp = gethostbyname(hostname)) != NULL) {
1305 local = *(u_int32_t *)hp->h_addr;
1306 if (local != 0 && !bad_ip_adrs(local))
1307 wo->ouraddr = local;
1311 if (demand && wo->hisaddr == 0) {
1312 option_error("remote IP address required for demand-dialling\n");
1316 if (demand && wo->accept_remote) {
1317 option_error("ipcp-accept-remote is incompatible with demand\n");
1325 * ip_demand_conf - configure the interface as though
1326 * IPCP were up, for use with dial-on-demand.
1332 ipcp_options *wo = &ipcp_wantoptions[u];
1334 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1338 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1340 if (wo->default_route)
1341 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1342 default_route_set[u] = 1;
1344 if (sifproxyarp(u, wo->hisaddr))
1345 proxy_arp_set[u] = 1;
1347 notice("local IP address %I", wo->ouraddr);
1348 notice("remote IP address %I", wo->hisaddr);
1355 * ipcp_up - IPCP has come UP.
1357 * Configure the IP network interface appropriately and bring it up.
1364 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1365 ipcp_options *go = &ipcp_gotoptions[f->unit];
1366 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1368 IPCPDEBUG(("ipcp: up"));
1371 * We must have a non-zero IP address for both ends of the link.
1374 ho->hisaddr = wo->hisaddr;
1376 if (ho->hisaddr == 0) {
1377 error("Could not determine remote IP address");
1378 ipcp_close(f->unit, "Could not determine remote IP address");
1381 if (go->ouraddr == 0) {
1382 error("Could not determine local IP address");
1383 ipcp_close(f->unit, "Could not determine local IP address");
1386 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr));
1387 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr));
1389 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1390 script_setenv("USEPEERDNS", "1");
1392 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]));
1394 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]));
1395 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1399 * Check that the peer is allowed to use the IP address it wants.
1401 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1402 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1403 ipcp_close(f->unit, "Unauthorized remote IP address");
1407 /* set tcp compression */
1408 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1411 * If we are doing dial-on-demand, the interface is already
1412 * configured, so we put out any saved-up packets, then set the
1413 * interface to pass IP packets.
1416 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1417 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1418 if (go->ouraddr != wo->ouraddr) {
1419 warn("Local IP address changed to %I", go->ouraddr);
1420 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr));
1421 wo->ouraddr = go->ouraddr;
1423 script_unsetenv("OLDIPLOCAL");
1424 if (ho->hisaddr != wo->hisaddr) {
1425 warn("Remote IP address changed to %I", ho->hisaddr);
1426 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr));
1427 wo->hisaddr = ho->hisaddr;
1429 script_unsetenv("OLDIPREMOTE");
1431 /* Set the interface to the new addresses */
1432 mask = GetMask(go->ouraddr);
1433 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1435 warn("Interface configuration failed");
1436 ipcp_close(f->unit, "Interface configuration failed");
1440 /* assign a default route through the interface if required */
1441 if (ipcp_wantoptions[f->unit].default_route)
1442 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1443 default_route_set[f->unit] = 1;
1445 /* Make a proxy ARP entry if requested. */
1446 if (ipcp_wantoptions[f->unit].proxy_arp)
1447 if (sifproxyarp(f->unit, ho->hisaddr))
1448 proxy_arp_set[f->unit] = 1;
1451 demand_rexmit(PPP_IP);
1452 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1456 * Set IP addresses and (if specified) netmask.
1458 mask = GetMask(go->ouraddr);
1460 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1461 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1463 warn("Interface configuration failed");
1464 ipcp_close(f->unit, "Interface configuration failed");
1469 /* bring the interface up for IP */
1470 if (!sifup(f->unit)) {
1472 warn("Interface failed to come up");
1473 ipcp_close(f->unit, "Interface configuration failed");
1477 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1478 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1480 warn("Interface configuration failed");
1481 ipcp_close(f->unit, "Interface configuration failed");
1485 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1487 /* assign a default route through the interface if required */
1488 if (ipcp_wantoptions[f->unit].default_route)
1489 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1490 default_route_set[f->unit] = 1;
1492 /* Make a proxy ARP entry if requested. */
1493 if (ipcp_wantoptions[f->unit].proxy_arp)
1494 if (sifproxyarp(f->unit, ho->hisaddr))
1495 proxy_arp_set[f->unit] = 1;
1497 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1499 notice("local IP address %I", go->ouraddr);
1500 notice("remote IP address %I", ho->hisaddr);
1502 notice("primary DNS address %I", go->dnsaddr[0]);
1504 notice("secondary DNS address %I", go->dnsaddr[1]);
1507 np_up(f->unit, PPP_IP);
1511 * Execute the ip-up script, like this:
1512 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1514 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1515 ipcp_script_state = s_up;
1516 ipcp_script(_PATH_IPUP);
1522 * ipcp_down - IPCP has gone DOWN.
1524 * Take the IP network interface down, clear its addresses
1525 * and delete routes through it.
1531 IPCPDEBUG(("ipcp: down"));
1532 /* XXX a bit IPv4-centric here, we only need to get the stats
1533 * before the interface is marked down. */
1534 update_link_stats(f->unit);
1537 np_down(f->unit, PPP_IP);
1539 sifvjcomp(f->unit, 0, 0, 0);
1542 * If we are doing dial-on-demand, set the interface
1543 * to queue up outgoing packets (for now).
1546 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1549 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1550 ipcp_hisoptions[f->unit].hisaddr);
1553 /* Execute the ip-down script */
1554 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1555 ipcp_script_state = s_down;
1556 ipcp_script(_PATH_IPDOWN);
1562 * ipcp_clear_addrs() - clear the interface addresses, routes,
1563 * proxy arp entries, etc.
1566 ipcp_clear_addrs(unit, ouraddr, hisaddr)
1568 u_int32_t ouraddr; /* local address */
1569 u_int32_t hisaddr; /* remote address */
1571 if (proxy_arp_set[unit]) {
1572 cifproxyarp(unit, hisaddr);
1573 proxy_arp_set[unit] = 0;
1575 if (default_route_set[unit]) {
1576 cifdefaultroute(unit, ouraddr, hisaddr);
1577 default_route_set[unit] = 0;
1579 cifaddr(unit, ouraddr, hisaddr);
1584 * ipcp_finished - possibly shut down the lower layers.
1590 np_finished(f->unit, PPP_IP);
1595 * ipcp_script_done - called when the ip-up or ip-down script
1599 ipcp_script_done(arg)
1602 ipcp_script_pid = 0;
1603 switch (ipcp_script_state) {
1605 if (ipcp_fsm[0].state != OPENED) {
1606 ipcp_script_state = s_down;
1607 ipcp_script(_PATH_IPDOWN);
1611 if (ipcp_fsm[0].state == OPENED) {
1612 ipcp_script_state = s_up;
1613 ipcp_script(_PATH_IPUP);
1621 * ipcp_script - Execute a script with arguments
1622 * interface-name tty-name speed local-IP remote-IP.
1628 char strspeed[32], strlocal[32], strremote[32];
1631 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1632 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
1633 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
1640 argv[5] = strremote;
1643 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done, NULL);
1647 * create_resolv - create the replacement resolv.conf file
1650 create_resolv(peerdns1, peerdns2)
1651 u_int32_t peerdns1, peerdns2;
1655 f = fopen(_PATH_RESOLV, "w");
1657 error("Failed to create %s: %m", _PATH_RESOLV);
1662 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
1665 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
1668 error("Write failed to %s: %m", _PATH_RESOLV);
1674 * ipcp_printpkt - print the contents of an IPCP packet.
1676 static char *ipcp_codenames[] = {
1677 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1678 "TermReq", "TermAck", "CodeRej"
1682 ipcp_printpkt(p, plen, printer, arg)
1685 void (*printer) __P((void *, char *, ...));
1688 int code, id, len, olen;
1689 u_char *pstart, *optend;
1693 if (plen < HEADERLEN)
1699 if (len < HEADERLEN || len > plen)
1702 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1703 printer(arg, " %s", ipcp_codenames[code-1]);
1705 printer(arg, " code=0x%x", code);
1706 printer(arg, " id=0x%x", id);
1713 /* print option list */
1718 if (olen < 2 || olen > len) {
1726 if (olen == CILEN_ADDRS) {
1729 printer(arg, "addrs %I", htonl(cilong));
1731 printer(arg, " %I", htonl(cilong));
1734 case CI_COMPRESSTYPE:
1735 if (olen >= CILEN_COMPRESS) {
1737 GETSHORT(cishort, p);
1738 printer(arg, "compress ");
1743 case IPCP_VJ_COMP_OLD:
1744 printer(arg, "old-VJ");
1747 printer(arg, "0x%x", cishort);
1752 if (olen == CILEN_ADDR) {
1755 printer(arg, "addr %I", htonl(cilong));
1762 printer(arg, "ms-dns%d %I", code - CI_MS_DNS1 + 1,
1769 printer(arg, "ms-wins %I", htonl(cilong));
1772 while (p < optend) {
1774 printer(arg, " %.2x", code);
1782 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1784 print_string(p, len, printer, arg);
1791 /* print the rest of the bytes in the packet */
1792 for (; len > 0; --len) {
1794 printer(arg, " %.2x", code);
1801 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1802 * We don't bring the link up for IP fragments or for TCP FIN packets
1805 #define IP_HDRLEN 20 /* bytes */
1806 #define IP_OFFMASK 0x1fff
1807 #define IPPROTO_TCP 6
1808 #define TCP_HDRLEN 20
1812 * We use these macros because the IP header may be at an odd address,
1813 * and some compilers might use word loads to get th_off or ip_hl.
1816 #define net_short(x) (((x)[0] << 8) + (x)[1])
1817 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1818 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1819 #define get_ipproto(x) (((unsigned char *)(x))[9])
1820 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1821 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1824 ip_active_pkt(pkt, len)
1833 if (len < IP_HDRLEN)
1835 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1837 if (get_ipproto(pkt) != IPPROTO_TCP)
1839 hlen = get_iphl(pkt) * 4;
1840 if (len < hlen + TCP_HDRLEN)
1843 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)