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
[] = "$FreeBSD$";
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
41 #include "pathnames.h"
44 ipcp_options ipcp_wantoptions
[NUM_PPP
]; /* Options that we want to request */
45 ipcp_options ipcp_gotoptions
[NUM_PPP
]; /* Options that peer ack'd */
46 ipcp_options ipcp_allowoptions
[NUM_PPP
]; /* Options we allow peer to request */
47 ipcp_options ipcp_hisoptions
[NUM_PPP
]; /* Options that we ack'd */
50 static int cis_received
[NUM_PPP
]; /* # Conf-Reqs received */
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 */
55 * Callbacks for fsm code. (CI = Configuration Information)
57 static void ipcp_resetci(fsm
*); /* Reset our CI */
58 static int ipcp_cilen(fsm
*); /* Return length of our CI */
59 static void ipcp_addci(fsm
*, u_char
*, int *); /* Add our CI */
60 static int ipcp_ackci(fsm
*, u_char
*, int); /* Peer ack'd our CI */
61 static int ipcp_nakci(fsm
*, u_char
*, int); /* Peer nak'd our CI */
62 static int ipcp_rejci(fsm
*, u_char
*, int); /* Peer rej'd our CI */
63 static int ipcp_reqci(fsm
*, u_char
*, int *, int); /* Rcv CI */
64 static void ipcp_up(fsm
*); /* We're UP */
65 static void ipcp_down(fsm
*); /* We're DOWN */
66 static void ipcp_script(fsm
*, char *); /* Run an up/down script */
67 static void ipcp_finished(fsm
*); /* Don't need lower layer */
69 fsm ipcp_fsm
[NUM_PPP
]; /* IPCP fsm structure */
71 static fsm_callbacks ipcp_callbacks
= { /* IPCP callback routines */
72 ipcp_resetci
, /* Reset our Configuration Information */
73 ipcp_cilen
, /* Length of our Configuration Information */
74 ipcp_addci
, /* Add our Configuration Information */
75 ipcp_ackci
, /* ACK our Configuration Information */
76 ipcp_nakci
, /* NAK our Configuration Information */
77 ipcp_rejci
, /* Reject our Configuration Information */
78 ipcp_reqci
, /* Request peer's Configuration Information */
79 ipcp_up
, /* Called when fsm reaches OPENED state */
80 ipcp_down
, /* Called when fsm leaves OPENED state */
81 NULL
, /* Called when we want the lower layer up */
82 ipcp_finished
, /* Called when we want the lower layer down */
83 NULL
, /* Called when Protocol-Reject received */
84 NULL
, /* Retransmission is necessary */
85 NULL
, /* Called to handle protocol-specific codes */
86 "IPCP" /* String name of protocol */
90 * Protocol entry points from main code.
92 static void ipcp_init(int);
93 static void ipcp_open(int);
94 static void ipcp_close(int, char *);
95 static void ipcp_lowerup(int);
96 static void ipcp_lowerdown(int);
97 static void ipcp_input(int, u_char
*, int);
98 static void ipcp_protrej(int);
99 static int ipcp_printpkt(u_char
*, int,
100 void (*) (void *, char *, ...), void *);
101 static void ip_check_options(void);
102 static int ip_demand_conf(int);
103 static int ip_active_pkt(u_char
*, int);
105 struct protent ipcp_protent
= {
123 static void ipcp_clear_addrs(int);
126 * Lengths of configuration options.
129 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
130 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
131 #define CILEN_ADDR 6 /* new-style single address option */
132 #define CILEN_ADDRS 10 /* old-style dual address option */
135 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
136 (x) == CONFNAK ? "NAK" : "REJ")
140 * Make a string representation of a network IP address.
148 ipaddr
= ntohl(ipaddr
);
150 sprintf(b
, "%d.%d.%d.%d",
151 (u_char
)(ipaddr
>> 24),
152 (u_char
)(ipaddr
>> 16),
153 (u_char
)(ipaddr
>> 8),
160 * ipcp_init - Initialize IPCP.
166 fsm
*f
= &ipcp_fsm
[unit
];
167 ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
168 ipcp_options
*ao
= &ipcp_allowoptions
[unit
];
171 f
->protocol
= PPP_IPCP
;
172 f
->callbacks
= &ipcp_callbacks
;
173 fsm_init(&ipcp_fsm
[unit
]);
175 memset(wo
, 0, sizeof(*wo
));
176 memset(ao
, 0, sizeof(*ao
));
180 wo
->vj_protocol
= IPCP_VJ_COMP
;
181 wo
->maxslotindex
= MAX_STATES
- 1; /* really max index */
184 /* max slots and slot-id compression are currently hardwired in */
185 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
190 ao
->maxslotindex
= MAX_STATES
- 1;
194 * XXX These control whether the user may use the proxyarp
195 * and defaultroute options.
198 ao
->default_route
= 1;
203 * ipcp_open - IPCP is allowed to come up.
209 fsm_open(&ipcp_fsm
[unit
]);
214 * ipcp_close - Take IPCP down.
217 ipcp_close(unit
, reason
)
221 fsm_close(&ipcp_fsm
[unit
], reason
);
226 * ipcp_lowerup - The lower layer is up.
232 fsm_lowerup(&ipcp_fsm
[unit
]);
237 * ipcp_lowerdown - The lower layer is down.
243 fsm_lowerdown(&ipcp_fsm
[unit
]);
248 * ipcp_input - Input IPCP packet.
251 ipcp_input(unit
, p
, len
)
256 fsm_input(&ipcp_fsm
[unit
], p
, len
);
261 * ipcp_protrej - A Protocol-Reject was received for IPCP.
263 * Pretend the lower layer went down, so we shut up.
269 fsm_lowerdown(&ipcp_fsm
[unit
]);
274 * ipcp_resetci - Reset our CI.
280 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
282 wo
->req_addr
= wo
->neg_addr
&& ipcp_allowoptions
[f
->unit
].neg_addr
;
283 if (wo
->ouraddr
== 0)
284 wo
->accept_local
= 1;
285 if (wo
->hisaddr
== 0)
286 wo
->accept_remote
= 1;
287 ipcp_gotoptions
[f
->unit
] = *wo
;
288 cis_received
[f
->unit
] = 0;
293 * ipcp_cilen - Return length of our CI.
299 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
300 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
301 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
303 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
304 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
307 * First see if we want to change our options to the old
308 * forms because we have received old forms from the peer.
310 if (wo
->neg_addr
&& !go
->neg_addr
&& !go
->old_addrs
) {
311 /* use the old style of address negotiation */
315 if (wo
->neg_vj
&& !go
->neg_vj
&& !go
->old_vj
) {
316 /* try an older style of VJ negotiation */
317 if (cis_received
[f
->unit
] == 0) {
318 /* keep trying the new style until we see some CI from the peer */
321 /* use the old style only if the peer did */
322 if (ho
->neg_vj
&& ho
->old_vj
) {
325 go
->vj_protocol
= ho
->vj_protocol
;
330 return (LENCIADDR(go
->neg_addr
, go
->old_addrs
) +
331 LENCIVJ(go
->neg_vj
, go
->old_vj
));
336 * ipcp_addci - Add our desired CIs to a packet.
339 ipcp_addci(f
, ucp
, lenp
)
344 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
347 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
349 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
350 if (len >= vjlen) { \
352 PUTCHAR(vjlen, ucp); \
353 PUTSHORT(val, ucp); \
355 PUTCHAR(maxslotindex, ucp); \
356 PUTCHAR(cflag, ucp); \
363 #define ADDCIADDR(opt, neg, old, val1, val2) \
365 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
366 if (len >= addrlen) { \
369 PUTCHAR(addrlen, ucp); \
381 ADDCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), go
->neg_addr
,
382 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
384 ADDCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
385 go
->maxslotindex
, go
->cflag
);
392 * ipcp_ackci - Ack our CIs.
399 ipcp_ackci(f
, p
, len
)
404 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
405 u_short cilen
, citype
, cishort
;
407 u_char cimaxslotindex
, cicflag
;
410 * CIs must be in exactly the same order that we sent...
411 * Check packet length and CI length at each step.
412 * If we find any deviations, then this packet is bad.
415 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
417 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
418 if ((len -= vjlen) < 0) \
420 GETCHAR(citype, p); \
422 if (cilen != vjlen || \
425 GETSHORT(cishort, p); \
426 if (cishort != val) \
429 GETCHAR(cimaxslotindex, p); \
430 if (cimaxslotindex != maxslotindex) \
432 GETCHAR(cicflag, p); \
433 if (cicflag != cflag) \
438 #define ACKCIADDR(opt, neg, old, val1, val2) \
440 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
442 if ((len -= addrlen) < 0) \
444 GETCHAR(citype, p); \
446 if (cilen != addrlen || \
451 if (val1 != cilong) \
456 if (val2 != cilong) \
461 ACKCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), go
->neg_addr
,
462 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
464 ACKCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
465 go
->maxslotindex
, go
->cflag
);
468 * If there are any remaining CIs, then this packet is bad.
475 IPCPDEBUG((LOG_INFO
, "ipcp_ackci: received bad Ack!"));
480 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
481 * This should not modify any state if the Nak is bad
482 * or if IPCP is in the OPENED state.
489 ipcp_nakci(f
, p
, len
)
494 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
495 u_char cimaxslotindex
, cicflag
;
496 u_char citype
, cilen
, *next
;
498 u_int32_t ciaddr1
, ciaddr2
, l
;
499 ipcp_options no
; /* options we've seen Naks for */
500 ipcp_options
try; /* options to request next time */
502 BZERO(&no
, sizeof(no
));
506 * Any Nak'd CIs must be in exactly the same order that we sent.
507 * Check packet length and CI length at each step.
508 * If we find any deviations, then this packet is bad.
510 #define NAKCIADDR(opt, neg, old, code) \
512 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
518 ciaddr1 = htonl(l); \
521 ciaddr2 = htonl(l); \
529 #define NAKCIVJ(opt, neg, code) \
531 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
536 GETSHORT(cishort, p); \
542 * Accept the peer's idea of {our,his} address, if different
543 * from our idea, only if the accept_{local,remote} flag is set.
545 NAKCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), neg_addr
, go
->old_addrs
,
546 if (go
->accept_local
&& ciaddr1
) { /* Do we know our address? */
547 try.ouraddr
= ciaddr1
;
548 IPCPDEBUG((LOG_INFO
, "local IP address %s",
551 if (go
->accept_remote
&& ciaddr2
) { /* Does he know his? */
552 try.hisaddr
= ciaddr2
;
553 IPCPDEBUG((LOG_INFO
, "remote IP address %s",
559 * Accept the peer's value of maxslotindex provided that it
560 * is less than what we asked for. Turn off slot-ID compression
561 * if the peer wants. Send old-style compress-type option if
564 NAKCIVJ(CI_COMPRESSTYPE
, neg_vj
,
565 if (cilen
== CILEN_VJ
) {
566 GETCHAR(cimaxslotindex
, p
);
568 if (cishort
== IPCP_VJ_COMP
) {
570 if (cimaxslotindex
< go
->maxslotindex
)
571 try.maxslotindex
= cimaxslotindex
;
578 if (cishort
== IPCP_VJ_COMP
|| cishort
== IPCP_VJ_COMP_OLD
) {
580 try.vj_protocol
= cishort
;
588 * There may be remaining CIs, if the peer is requesting negotiation
589 * on an option that we didn't include in our request packet.
590 * If they want to negotiate about IP addresses, we comply.
591 * If they want us to ask for compression, we refuse.
593 while (len
> CILEN_VOID
) {
596 if( (len
-= cilen
) < 0 )
598 next
= p
+ cilen
- 2;
601 case CI_COMPRESSTYPE
:
602 if (go
->neg_vj
|| no
.neg_vj
||
603 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
))
608 if ((go
->neg_addr
&& go
->old_addrs
) || no
.old_addrs
609 || cilen
!= CILEN_ADDRS
)
615 if (ciaddr1
&& go
->accept_local
)
616 try.ouraddr
= ciaddr1
;
619 if (ciaddr2
&& go
->accept_remote
)
620 try.hisaddr
= ciaddr2
;
624 if (go
->neg_addr
|| no
.neg_addr
|| cilen
!= CILEN_ADDR
)
629 if (ciaddr1
&& go
->accept_local
)
630 try.ouraddr
= ciaddr1
;
631 if (try.ouraddr
!= 0)
639 /* If there is still anything left, this packet is bad. */
644 * OK, the Nak is good. Now we can update state.
646 if (f
->state
!= OPENED
)
652 IPCPDEBUG((LOG_INFO
, "ipcp_nakci: received bad Nak!"));
658 * ipcp_rejci - Reject some of our CIs.
661 ipcp_rejci(f
, p
, len
)
666 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
667 u_char cimaxslotindex
, ciflag
, cilen
;
670 ipcp_options
try; /* options to request next time */
674 * Any Rejected CIs must be in exactly the same order that we sent.
675 * Check packet length and CI length at each step.
676 * If we find any deviations, then this packet is bad.
678 #define REJCIADDR(opt, neg, old, val1, val2) \
680 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
688 /* Check rejected value. */ \
689 if (cilong != val1) \
694 /* Check rejected value. */ \
695 if (cilong != val2) \
701 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
703 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
708 GETSHORT(cishort, p); \
709 /* Check rejected value. */ \
710 if (cishort != val) \
713 GETCHAR(cimaxslotindex, p); \
714 if (cimaxslotindex != maxslot) \
716 GETCHAR(ciflag, p); \
717 if (ciflag != cflag) \
723 REJCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), neg_addr
,
724 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
726 REJCIVJ(CI_COMPRESSTYPE
, neg_vj
, go
->vj_protocol
, go
->old_vj
,
727 go
->maxslotindex
, go
->cflag
);
730 * If there are any remaining CIs, then this packet is bad.
735 * Now we can update state.
737 if (f
->state
!= OPENED
)
742 IPCPDEBUG((LOG_INFO
, "ipcp_rejci: received bad Reject!"));
748 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
750 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
751 * appropriately. If reject_if_disagree is non-zero, doesn't return
752 * CONFNAK; returns CONFREJ if it can't return CONFACK.
755 ipcp_reqci(f
, inp
, len
, reject_if_disagree
)
757 u_char
*inp
; /* Requested CIs */
758 int *len
; /* Length of requested CIs */
759 int reject_if_disagree
;
761 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
762 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
763 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
764 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
765 u_char
*cip
, *next
; /* Pointer to current and next CIs */
766 u_short cilen
, citype
; /* Parsed len, type */
767 u_short cishort
; /* Parsed short value */
768 u_int32_t tl
, ciaddr1
, ciaddr2
;/* Parsed address values */
769 int rc
= CONFACK
; /* Final packet return code */
770 int orc
; /* Individual option return code */
771 u_char
*p
; /* Pointer to next char to parse */
772 u_char
*ucp
= inp
; /* Pointer to current output char */
773 int l
= *len
; /* Length left */
774 u_char maxslotindex
, cflag
;
777 cis_received
[f
->unit
] = 1;
780 * Reset all his options.
782 BZERO(ho
, sizeof(*ho
));
785 * Process all his options.
789 orc
= CONFACK
; /* Assume success */
790 cip
= p
= next
; /* Remember begining of CI */
791 if (l
< 2 || /* Not enough data for CI header or */
792 p
[1] < 2 || /* CI length too small or */
793 p
[1] > l
) { /* CI length too big? */
794 IPCPDEBUG((LOG_INFO
, "ipcp_reqci: bad CI length!"));
795 orc
= CONFREJ
; /* Reject bad CI */
796 cilen
= l
; /* Reject till end of packet */
797 l
= 0; /* Don't loop again */
800 GETCHAR(citype
, p
); /* Parse CI type */
801 GETCHAR(cilen
, p
); /* Parse CI length */
802 l
-= cilen
; /* Adjust remaining length */
803 next
+= cilen
; /* Step to next CI */
805 switch (citype
) { /* Check CI type */
807 IPCPDEBUG((LOG_INFO
, "ipcp: received ADDRS "));
809 cilen
!= CILEN_ADDRS
) { /* Check CI length */
810 orc
= CONFREJ
; /* Reject CI */
815 * If he has no address, or if we both have his address but
816 * disagree about it, then NAK it with our idea.
817 * In particular, if we don't know his address, but he does,
820 GETLONG(tl
, p
); /* Parse source address (his) */
822 IPCPDEBUG((LOG_INFO
, "(%s:", ip_ntoa(ciaddr1
)));
823 if (ciaddr1
!= wo
->hisaddr
824 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
826 if (!reject_if_disagree
) {
827 DECPTR(sizeof(u_int32_t
), p
);
828 tl
= ntohl(wo
->hisaddr
);
831 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
833 * If neither we nor he knows his address, reject the option.
836 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
841 * If he doesn't know our address, or if we both have our address
842 * but disagree about it, then NAK it with our idea.
844 GETLONG(tl
, p
); /* Parse desination address (ours) */
846 IPCPDEBUG((LOG_INFO
, "%s)", ip_ntoa(ciaddr2
)));
847 if (ciaddr2
!= wo
->ouraddr
) {
848 if (ciaddr2
== 0 || !wo
->accept_local
) {
850 if (!reject_if_disagree
) {
851 DECPTR(sizeof(u_int32_t
), p
);
852 tl
= ntohl(wo
->ouraddr
);
856 go
->ouraddr
= ciaddr2
; /* accept peer's idea */
862 ho
->hisaddr
= ciaddr1
;
863 ho
->ouraddr
= ciaddr2
;
867 IPCPDEBUG((LOG_INFO
, "ipcp: received ADDR "));
870 cilen
!= CILEN_ADDR
) { /* Check CI length */
871 orc
= CONFREJ
; /* Reject CI */
876 * If he has no address, or if we both have his address but
877 * disagree about it, then NAK it with our idea.
878 * In particular, if we don't know his address, but he does,
881 GETLONG(tl
, p
); /* Parse source address (his) */
883 IPCPDEBUG((LOG_INFO
, "(%s)", ip_ntoa(ciaddr1
)));
884 if (ciaddr1
!= wo
->hisaddr
885 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
887 if (!reject_if_disagree
) {
888 DECPTR(sizeof(u_int32_t
), p
);
889 tl
= ntohl(wo
->hisaddr
);
892 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
894 * Don't ACK an address of 0.0.0.0 - reject it instead.
897 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
902 ho
->hisaddr
= ciaddr1
;
907 /* Microsoft primary or secondary DNS request */
908 d
= citype
== CI_MS_DNS2
;
909 IPCPDEBUG((LOG_INFO
, "ipcp: received DNS%d Request ", d
+1));
911 /* If we do not have a DNS address then we cannot send it */
912 if (ao
->dnsaddr
[d
] == 0 ||
913 cilen
!= CILEN_ADDR
) { /* Check CI length */
914 orc
= CONFREJ
; /* Reject CI */
918 if (htonl(tl
) != ao
->dnsaddr
[d
]) {
919 DECPTR(sizeof(u_int32_t
), p
);
920 tl
= ntohl(ao
->dnsaddr
[d
]);
928 /* Microsoft primary or secondary WINS request */
929 d
= citype
== CI_MS_WINS2
;
930 IPCPDEBUG((LOG_INFO
, "ipcp: received WINS%d Request ", d
+1));
932 /* If we do not have a DNS address then we cannot send it */
933 if (ao
->winsaddr
[d
] == 0 ||
934 cilen
!= CILEN_ADDR
) { /* Check CI length */
935 orc
= CONFREJ
; /* Reject CI */
939 if (htonl(tl
) != ao
->winsaddr
[d
]) {
940 DECPTR(sizeof(u_int32_t
), p
);
941 tl
= ntohl(ao
->winsaddr
[d
]);
947 case CI_COMPRESSTYPE
:
948 IPCPDEBUG((LOG_INFO
, "ipcp: received COMPRESSTYPE "));
950 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
)) {
954 GETSHORT(cishort
, p
);
955 IPCPDEBUG((LOG_INFO
, "(%d)", cishort
));
957 if (!(cishort
== IPCP_VJ_COMP
||
958 (cishort
== IPCP_VJ_COMP_OLD
&& cilen
== CILEN_COMPRESS
))) {
964 ho
->vj_protocol
= cishort
;
965 if (cilen
== CILEN_VJ
) {
966 GETCHAR(maxslotindex
, p
);
967 if (maxslotindex
> ao
->maxslotindex
) {
969 if (!reject_if_disagree
){
971 PUTCHAR(ao
->maxslotindex
, p
);
975 if (cflag
&& !ao
->cflag
) {
977 if (!reject_if_disagree
){
979 PUTCHAR(wo
->cflag
, p
);
982 ho
->maxslotindex
= maxslotindex
;
986 ho
->maxslotindex
= MAX_STATES
- 1;
997 IPCPDEBUG((LOG_INFO
, " (%s)\n", CODENAME(orc
)));
999 if (orc
== CONFACK
&& /* Good CI */
1000 rc
!= CONFACK
) /* but prior CI wasnt? */
1001 continue; /* Don't send this one */
1003 if (orc
== CONFNAK
) { /* Nak this CI? */
1004 if (reject_if_disagree
) /* Getting fed up with sending NAKs? */
1005 orc
= CONFREJ
; /* Get tough if so */
1007 if (rc
== CONFREJ
) /* Rejecting prior CI? */
1008 continue; /* Don't send this one */
1009 if (rc
== CONFACK
) { /* Ack'd all prior CIs? */
1010 rc
= CONFNAK
; /* Not anymore... */
1011 ucp
= inp
; /* Backup */
1016 if (orc
== CONFREJ
&& /* Reject this CI */
1017 rc
!= CONFREJ
) { /* but no prior ones? */
1019 ucp
= inp
; /* Backup */
1022 /* Need to move CI? */
1024 BCOPY(cip
, ucp
, cilen
); /* Move it */
1026 /* Update output pointer */
1031 * If we aren't rejecting this packet, and we want to negotiate
1032 * their address, and they didn't send their address, then we
1033 * send a NAK with a CI_ADDR option appended. We assume the
1034 * input buffer is long enough that we can append the extra
1037 if (rc
!= CONFREJ
&& !ho
->neg_addr
&&
1038 wo
->req_addr
&& !reject_if_disagree
) {
1039 if (rc
== CONFACK
) {
1041 ucp
= inp
; /* reset pointer */
1042 wo
->req_addr
= 0; /* don't ask again */
1044 PUTCHAR(CI_ADDR
, ucp
);
1045 PUTCHAR(CILEN_ADDR
, ucp
);
1046 tl
= ntohl(wo
->hisaddr
);
1050 *len
= ucp
- inp
; /* Compute output length */
1051 IPCPDEBUG((LOG_INFO
, "ipcp: returning Configure-%s", CODENAME(rc
)));
1052 return (rc
); /* Return final code */
1057 * ip_check_options - check that any IP-related options are OK,
1058 * and assign appropriate defaults.
1065 ipcp_options
*wo
= &ipcp_wantoptions
[0];
1068 * Default our local IP address based on our hostname.
1069 * If local IP address already given, don't bother.
1071 if (wo
->ouraddr
== 0 && !disable_defaultip
) {
1073 * Look up our hostname (possibly with domain name appended)
1074 * and take the first IP address as our local IP address.
1075 * If there isn't an IP address for our hostname, too bad.
1077 wo
->accept_local
= 1; /* don't insist on this default value */
1078 if ((hp
= gethostbyname(hostname
)) != NULL
) {
1079 local
= *(u_int32_t
*)hp
->h_addr
;
1080 if (local
!= 0 && !bad_ip_adrs(local
))
1081 wo
->ouraddr
= local
;
1085 if (demand
&& wo
->hisaddr
== 0) {
1086 option_error("remote IP address required for demand-dialling\n");
1090 if (demand
&& wo
->accept_remote
) {
1091 option_error("ipcp-accept-remote is incompatible with demand\n");
1099 * ip_demand_conf - configure the interface as though
1100 * IPCP were up, for use with dial-on-demand.
1106 ipcp_options
*wo
= &ipcp_wantoptions
[u
];
1108 if (!sifaddr(u
, wo
->ouraddr
, wo
->hisaddr
, GetMask(wo
->ouraddr
)))
1112 if (!sifnpmode(u
, PPP_IP
, NPMODE_QUEUE
))
1114 if (wo
->default_route
)
1115 if (sifdefaultroute(u
, wo
->ouraddr
, wo
->hisaddr
))
1116 default_route_set
[u
] = 1;
1118 if (sifproxyarp(u
, wo
->hisaddr
))
1119 proxy_arp_set
[u
] = 1;
1121 syslog(LOG_NOTICE
, "local IP address %s", ip_ntoa(wo
->ouraddr
));
1122 syslog(LOG_NOTICE
, "remote IP address %s", ip_ntoa(wo
->hisaddr
));
1129 * ipcp_up - IPCP has come UP.
1131 * Configure the IP network interface appropriately and bring it up.
1138 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1139 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1140 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1142 np_up(f
->unit
, PPP_IP
);
1143 IPCPDEBUG((LOG_INFO
, "ipcp: up"));
1146 * We must have a non-zero IP address for both ends of the link.
1149 ho
->hisaddr
= wo
->hisaddr
;
1151 if (ho
->hisaddr
== 0) {
1152 syslog(LOG_ERR
, "Could not determine remote IP address");
1153 ipcp_close(f
->unit
, "Could not determine remote IP address");
1156 if (go
->ouraddr
== 0) {
1157 syslog(LOG_ERR
, "Could not determine local IP address");
1158 ipcp_close(f
->unit
, "Could not determine local IP address");
1161 script_setenv("IPLOCAL", ip_ntoa(go
->ouraddr
));
1162 script_setenv("IPREMOTE", ip_ntoa(ho
->hisaddr
));
1165 * Check that the peer is allowed to use the IP address it wants.
1167 if (!auth_ip_addr(f
->unit
, ho
->hisaddr
)) {
1168 syslog(LOG_ERR
, "Peer is not authorized to use remote address %s",
1169 ip_ntoa(ho
->hisaddr
));
1170 ipcp_close(f
->unit
, "Unauthorized remote IP address");
1174 /* set tcp compression */
1175 sifvjcomp(f
->unit
, ho
->neg_vj
, ho
->cflag
, ho
->maxslotindex
);
1178 * If we are doing dial-on-demand, the interface is already
1179 * configured, so we put out any saved-up packets, then set the
1180 * interface to pass IP packets.
1183 if (go
->ouraddr
!= wo
->ouraddr
|| ho
->hisaddr
!= wo
->hisaddr
) {
1184 if (go
->ouraddr
!= wo
->ouraddr
)
1185 syslog(LOG_WARNING
, "Local IP address changed to %s",
1186 ip_ntoa(go
->ouraddr
));
1187 if (ho
->hisaddr
!= wo
->hisaddr
)
1188 syslog(LOG_WARNING
, "Remote IP address changed to %s",
1189 ip_ntoa(ho
->hisaddr
));
1190 ipcp_clear_addrs(f
->unit
);
1192 /* Set the interface to the new addresses */
1193 mask
= GetMask(go
->ouraddr
);
1194 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1195 IPCPDEBUG((LOG_WARNING
, "sifaddr failed"));
1196 ipcp_close(f
->unit
, "Interface configuration failed");
1200 /* assign a default route through the interface if required */
1201 if (ipcp_wantoptions
[f
->unit
].default_route
)
1202 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1203 default_route_set
[f
->unit
] = 1;
1205 /* Make a proxy ARP entry if requested. */
1206 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1207 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1208 proxy_arp_set
[f
->unit
] = 1;
1211 demand_rexmit(PPP_IP
);
1212 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1216 * Set IP addresses and (if specified) netmask.
1218 mask
= GetMask(go
->ouraddr
);
1220 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1221 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1222 IPCPDEBUG((LOG_WARNING
, "sifaddr failed"));
1223 ipcp_close(f
->unit
, "Interface configuration failed");
1228 /* bring the interface up for IP */
1229 if (!sifup(f
->unit
)) {
1230 IPCPDEBUG((LOG_WARNING
, "sifup failed"));
1231 ipcp_close(f
->unit
, "Interface configuration failed");
1235 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1236 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1237 IPCPDEBUG((LOG_WARNING
, "sifaddr failed"));
1238 ipcp_close(f
->unit
, "Interface configuration failed");
1242 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1244 /* assign a default route through the interface if required */
1245 if (ipcp_wantoptions
[f
->unit
].default_route
)
1246 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1247 default_route_set
[f
->unit
] = 1;
1249 /* Make a proxy ARP entry if requested. */
1250 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1251 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1252 proxy_arp_set
[f
->unit
] = 1;
1254 syslog(LOG_NOTICE
, "local IP address %s", ip_ntoa(go
->ouraddr
));
1255 syslog(LOG_NOTICE
, "remote IP address %s", ip_ntoa(ho
->hisaddr
));
1259 * Execute the ip-up script, like this:
1260 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1262 ipcp_script(f
, _PATH_IPUP
);
1268 * ipcp_down - IPCP has gone DOWN.
1270 * Take the IP network interface down, clear its addresses
1271 * and delete routes through it.
1277 IPCPDEBUG((LOG_INFO
, "ipcp: down"));
1278 np_down(f
->unit
, PPP_IP
);
1279 sifvjcomp(f
->unit
, 0, 0, 0);
1282 * If we are doing dial-on-demand, set the interface
1283 * to queue up outgoing packets (for now).
1286 sifnpmode(f
->unit
, PPP_IP
, NPMODE_QUEUE
);
1289 ipcp_clear_addrs(f
->unit
);
1292 /* Execute the ip-down script */
1293 ipcp_script(f
, _PATH_IPDOWN
);
1298 * ipcp_clear_addrs() - clear the interface addresses, routes,
1299 * proxy arp entries, etc.
1302 ipcp_clear_addrs(unit
)
1305 u_int32_t ouraddr
, hisaddr
;
1307 ouraddr
= ipcp_gotoptions
[unit
].ouraddr
;
1308 hisaddr
= ipcp_hisoptions
[unit
].hisaddr
;
1309 if (proxy_arp_set
[unit
]) {
1310 cifproxyarp(unit
, hisaddr
);
1311 proxy_arp_set
[unit
] = 0;
1313 if (default_route_set
[unit
]) {
1314 cifdefaultroute(unit
, ouraddr
, hisaddr
);
1315 default_route_set
[unit
] = 0;
1317 cifaddr(unit
, ouraddr
, hisaddr
);
1322 * ipcp_finished - possibly shut down the lower layers.
1328 np_finished(f
->unit
, PPP_IP
);
1333 * ipcp_script - Execute a script with arguments
1334 * interface-name tty-name speed local-IP remote-IP.
1337 ipcp_script(f
, script
)
1341 char strspeed
[32], strlocal
[32], strremote
[32];
1344 sprintf(strspeed
, "%d", baud_rate
);
1345 strcpy(strlocal
, ip_ntoa(ipcp_gotoptions
[f
->unit
].ouraddr
));
1346 strcpy(strremote
, ip_ntoa(ipcp_hisoptions
[f
->unit
].hisaddr
));
1353 argv
[5] = strremote
;
1356 run_program(script
, argv
, 0);
1360 * ipcp_printpkt - print the contents of an IPCP packet.
1362 static char *ipcp_codenames
[] = {
1363 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1364 "TermReq", "TermAck", "CodeRej"
1368 ipcp_printpkt(p
, plen
, printer
, arg
)
1371 void (*printer
)(void *, char *, ...);
1374 int code
, id
, len
, olen
;
1375 u_char
*pstart
, *optend
;
1379 if (plen
< HEADERLEN
)
1385 if (len
< HEADERLEN
|| len
> plen
)
1388 if (code
>= 1 && code
<= sizeof(ipcp_codenames
) / sizeof(char *))
1389 printer(arg
, " %s", ipcp_codenames
[code
-1]);
1391 printer(arg
, " code=0x%x", code
);
1392 printer(arg
, " id=0x%x", id
);
1399 /* print option list */
1404 if (olen
< 2 || olen
> len
) {
1412 if (olen
== CILEN_ADDRS
) {
1415 printer(arg
, "addrs %I", htonl(cilong
));
1417 printer(arg
, " %I", htonl(cilong
));
1420 case CI_COMPRESSTYPE
:
1421 if (olen
>= CILEN_COMPRESS
) {
1423 GETSHORT(cishort
, p
);
1424 printer(arg
, "compress ");
1429 case IPCP_VJ_COMP_OLD
:
1430 printer(arg
, "old-VJ");
1433 printer(arg
, "0x%x", cishort
);
1438 if (olen
== CILEN_ADDR
) {
1441 printer(arg
, "addr %I", htonl(cilong
));
1448 printer(arg
, "ms-dns %I", htonl(cilong
));
1454 printer(arg
, "ms-wins %I", htonl(cilong
));
1457 while (p
< optend
) {
1459 printer(arg
, " %.2x", code
);
1467 if (len
> 0 && *p
>= ' ' && *p
< 0x7f) {
1469 print_string(p
, len
, printer
, arg
);
1476 /* print the rest of the bytes in the packet */
1477 for (; len
> 0; --len
) {
1479 printer(arg
, " %.2x", code
);
1486 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1487 * We don't bring the link up for IP fragments or for TCP FIN packets
1490 #define IP_HDRLEN 20 /* bytes */
1491 #define IP_OFFMASK 0x1fff
1492 #define IPPROTO_TCP 6
1493 #define TCP_HDRLEN 20
1497 * We use these macros because the IP header may be at an odd address,
1498 * and some compilers might use word loads to get th_off or ip_hl.
1501 #define net_short(x) (((x)[0] << 8) + (x)[1])
1502 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1503 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1504 #define get_ipproto(x) (((unsigned char *)(x))[9])
1505 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1506 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1509 ip_active_pkt(pkt
, len
)
1518 if (len
< IP_HDRLEN
)
1520 if ((get_ipoff(pkt
) & IP_OFFMASK
) != 0)
1522 if (get_ipproto(pkt
) != IPPROTO_TCP
)
1524 hlen
= get_iphl(pkt
) * 4;
1525 if (len
< hlen
+ TCP_HDRLEN
)
1528 if ((get_tcpflags(tcp
) & TH_FIN
) != 0 && len
== hlen
+ get_tcpoff(tcp
) * 4)