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.5 2007/11/25 01:28:24 swildner Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.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 */
49 static int cis_received
[NUM_PPP
]; /* # Conf-Reqs received */
50 static int default_route_set
[NUM_PPP
]; /* Have set up a default route */
51 static int proxy_arp_set
[NUM_PPP
]; /* Have created proxy arp entry */
54 * Callbacks for fsm code. (CI = Configuration Information)
56 static void ipcp_resetci(fsm
*); /* Reset our CI */
57 static int ipcp_cilen(fsm
*); /* Return length of our CI */
58 static void ipcp_addci(fsm
*, u_char
*, int *); /* Add our CI */
59 static int ipcp_ackci(fsm
*, u_char
*, int); /* Peer ack'd our CI */
60 static int ipcp_nakci(fsm
*, u_char
*, int); /* Peer nak'd our CI */
61 static int ipcp_rejci(fsm
*, u_char
*, int); /* Peer rej'd our CI */
62 static int ipcp_reqci(fsm
*, u_char
*, int *, int); /* Rcv CI */
63 static void ipcp_up(fsm
*); /* We're UP */
64 static void ipcp_down(fsm
*); /* We're DOWN */
65 static void ipcp_script(fsm
*, char *); /* Run an up/down script */
66 static void ipcp_finished(fsm
*); /* Don't need lower layer */
68 fsm ipcp_fsm
[NUM_PPP
]; /* IPCP fsm structure */
70 static fsm_callbacks ipcp_callbacks
= { /* IPCP callback routines */
71 ipcp_resetci
, /* Reset our Configuration Information */
72 ipcp_cilen
, /* Length of our Configuration Information */
73 ipcp_addci
, /* Add our Configuration Information */
74 ipcp_ackci
, /* ACK our Configuration Information */
75 ipcp_nakci
, /* NAK our Configuration Information */
76 ipcp_rejci
, /* Reject our Configuration Information */
77 ipcp_reqci
, /* Request peer's Configuration Information */
78 ipcp_up
, /* Called when fsm reaches OPENED state */
79 ipcp_down
, /* Called when fsm leaves OPENED state */
80 NULL
, /* Called when we want the lower layer up */
81 ipcp_finished
, /* Called when we want the lower layer down */
82 NULL
, /* Called when Protocol-Reject received */
83 NULL
, /* Retransmission is necessary */
84 NULL
, /* Called to handle protocol-specific codes */
85 "IPCP" /* String name of protocol */
89 * Protocol entry points from main code.
91 static void ipcp_init(int);
92 static void ipcp_open(int);
93 static void ipcp_close(int, char *);
94 static void ipcp_lowerup(int);
95 static void ipcp_lowerdown(int);
96 static void ipcp_input(int, u_char
*, int);
97 static void ipcp_protrej(int);
98 static int ipcp_printpkt(u_char
*, int,
99 void (*)(void *, char *, ...), void *);
100 static void ip_check_options(void);
101 static int ip_demand_conf(int);
102 static int ip_active_pkt(u_char
*, int);
104 struct protent ipcp_protent
= {
122 static void ipcp_clear_addrs(int);
125 * Lengths of configuration options.
128 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
129 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
130 #define CILEN_ADDR 6 /* new-style single address option */
131 #define CILEN_ADDRS 10 /* old-style dual address option */
134 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
135 (x) == CONFNAK ? "NAK" : "REJ")
139 * Make a string representation of a network IP address.
142 ip_ntoa(u_int32_t ipaddr
)
146 ipaddr
= ntohl(ipaddr
);
148 sprintf(b
, "%d.%d.%d.%d",
149 (u_char
)(ipaddr
>> 24),
150 (u_char
)(ipaddr
>> 16),
151 (u_char
)(ipaddr
>> 8),
158 * ipcp_init - Initialize IPCP.
163 fsm
*f
= &ipcp_fsm
[unit
];
164 ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
165 ipcp_options
*ao
= &ipcp_allowoptions
[unit
];
168 f
->protocol
= PPP_IPCP
;
169 f
->callbacks
= &ipcp_callbacks
;
170 fsm_init(&ipcp_fsm
[unit
]);
172 memset(wo
, 0, sizeof(*wo
));
173 memset(ao
, 0, sizeof(*ao
));
177 wo
->vj_protocol
= IPCP_VJ_COMP
;
178 wo
->maxslotindex
= MAX_STATES
- 1; /* really max index */
181 /* max slots and slot-id compression are currently hardwired in */
182 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
187 ao
->maxslotindex
= MAX_STATES
- 1;
191 * XXX These control whether the user may use the proxyarp
192 * and defaultroute options.
195 ao
->default_route
= 1;
200 * ipcp_open - IPCP is allowed to come up.
205 fsm_open(&ipcp_fsm
[unit
]);
210 * ipcp_close - Take IPCP down.
213 ipcp_close(int unit
, char *reason
)
215 fsm_close(&ipcp_fsm
[unit
], reason
);
220 * ipcp_lowerup - The lower layer is up.
223 ipcp_lowerup(int unit
)
225 fsm_lowerup(&ipcp_fsm
[unit
]);
230 * ipcp_lowerdown - The lower layer is down.
233 ipcp_lowerdown(int unit
)
235 fsm_lowerdown(&ipcp_fsm
[unit
]);
240 * ipcp_input - Input IPCP packet.
243 ipcp_input(int unit
, u_char
*p
, int len
)
245 fsm_input(&ipcp_fsm
[unit
], p
, len
);
250 * ipcp_protrej - A Protocol-Reject was received for IPCP.
252 * Pretend the lower layer went down, so we shut up.
255 ipcp_protrej(int unit
)
257 fsm_lowerdown(&ipcp_fsm
[unit
]);
262 * ipcp_resetci - Reset our CI.
267 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
269 wo
->req_addr
= wo
->neg_addr
&& ipcp_allowoptions
[f
->unit
].neg_addr
;
270 if (wo
->ouraddr
== 0)
271 wo
->accept_local
= 1;
272 if (wo
->hisaddr
== 0)
273 wo
->accept_remote
= 1;
274 ipcp_gotoptions
[f
->unit
] = *wo
;
275 cis_received
[f
->unit
] = 0;
280 * ipcp_cilen - Return length of our CI.
285 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
286 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
287 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
289 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
290 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
293 * First see if we want to change our options to the old
294 * forms because we have received old forms from the peer.
296 if (wo
->neg_addr
&& !go
->neg_addr
&& !go
->old_addrs
) {
297 /* use the old style of address negotiation */
301 if (wo
->neg_vj
&& !go
->neg_vj
&& !go
->old_vj
) {
302 /* try an older style of VJ negotiation */
303 if (cis_received
[f
->unit
] == 0) {
304 /* keep trying the new style until we see some CI from the peer */
307 /* use the old style only if the peer did */
308 if (ho
->neg_vj
&& ho
->old_vj
) {
311 go
->vj_protocol
= ho
->vj_protocol
;
316 return (LENCIADDR(go
->neg_addr
, go
->old_addrs
) +
317 LENCIVJ(go
->neg_vj
, go
->old_vj
));
322 * ipcp_addci - Add our desired CIs to a packet.
325 ipcp_addci(fsm
*f
, u_char
*ucp
, int *lenp
)
327 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
330 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
332 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
333 if (len >= vjlen) { \
335 PUTCHAR(vjlen, ucp); \
336 PUTSHORT(val, ucp); \
338 PUTCHAR(maxslotindex, ucp); \
339 PUTCHAR(cflag, ucp); \
346 #define ADDCIADDR(opt, neg, old, val1, val2) \
348 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
349 if (len >= addrlen) { \
352 PUTCHAR(addrlen, ucp); \
364 ADDCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), go
->neg_addr
,
365 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
367 ADDCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
368 go
->maxslotindex
, go
->cflag
);
375 * ipcp_ackci - Ack our CIs.
382 ipcp_ackci(fsm
*f
, u_char
*p
, int len
)
384 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
385 u_short cilen
, citype
, cishort
;
387 u_char cimaxslotindex
, cicflag
;
390 * CIs must be in exactly the same order that we sent...
391 * Check packet length and CI length at each step.
392 * If we find any deviations, then this packet is bad.
395 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
397 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
398 if ((len -= vjlen) < 0) \
400 GETCHAR(citype, p); \
402 if (cilen != vjlen || \
405 GETSHORT(cishort, p); \
406 if (cishort != val) \
409 GETCHAR(cimaxslotindex, p); \
410 if (cimaxslotindex != maxslotindex) \
412 GETCHAR(cicflag, p); \
413 if (cicflag != cflag) \
418 #define ACKCIADDR(opt, neg, old, val1, val2) \
420 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
422 if ((len -= addrlen) < 0) \
424 GETCHAR(citype, p); \
426 if (cilen != addrlen || \
431 if (val1 != cilong) \
436 if (val2 != cilong) \
441 ACKCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), go
->neg_addr
,
442 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
444 ACKCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
445 go
->maxslotindex
, go
->cflag
);
448 * If there are any remaining CIs, then this packet is bad.
455 IPCPDEBUG((LOG_INFO
, "ipcp_ackci: received bad Ack!"));
460 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
461 * This should not modify any state if the Nak is bad
462 * or if IPCP is in the OPENED state.
469 ipcp_nakci(fsm
*f
, u_char
*p
, int len
)
471 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
472 u_char cimaxslotindex
, cicflag
;
473 u_char citype
, cilen
, *next
;
475 u_int32_t ciaddr1
, ciaddr2
, l
;
476 ipcp_options no
; /* options we've seen Naks for */
477 ipcp_options
try; /* options to request next time */
479 BZERO(&no
, sizeof(no
));
483 * Any Nak'd CIs must be in exactly the same order that we sent.
484 * Check packet length and CI length at each step.
485 * If we find any deviations, then this packet is bad.
487 #define NAKCIADDR(opt, neg, old, code) \
489 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
495 ciaddr1 = htonl(l); \
498 ciaddr2 = htonl(l); \
506 #define NAKCIVJ(opt, neg, code) \
508 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
513 GETSHORT(cishort, p); \
519 * Accept the peer's idea of {our,his} address, if different
520 * from our idea, only if the accept_{local,remote} flag is set.
522 NAKCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), neg_addr
, go
->old_addrs
,
523 if (go
->accept_local
&& ciaddr1
) { /* Do we know our address? */
524 try.ouraddr
= ciaddr1
;
525 IPCPDEBUG((LOG_INFO
, "local IP address %s",
528 if (go
->accept_remote
&& ciaddr2
) { /* Does he know his? */
529 try.hisaddr
= ciaddr2
;
530 IPCPDEBUG((LOG_INFO
, "remote IP address %s",
536 * Accept the peer's value of maxslotindex provided that it
537 * is less than what we asked for. Turn off slot-ID compression
538 * if the peer wants. Send old-style compress-type option if
541 NAKCIVJ(CI_COMPRESSTYPE
, neg_vj
,
542 if (cilen
== CILEN_VJ
) {
543 GETCHAR(cimaxslotindex
, p
);
545 if (cishort
== IPCP_VJ_COMP
) {
547 if (cimaxslotindex
< go
->maxslotindex
)
548 try.maxslotindex
= cimaxslotindex
;
555 if (cishort
== IPCP_VJ_COMP
|| cishort
== IPCP_VJ_COMP_OLD
) {
557 try.vj_protocol
= cishort
;
565 * There may be remaining CIs, if the peer is requesting negotiation
566 * on an option that we didn't include in our request packet.
567 * If they want to negotiate about IP addresses, we comply.
568 * If they want us to ask for compression, we refuse.
570 while (len
> CILEN_VOID
) {
573 if( (len
-= cilen
) < 0 )
575 next
= p
+ cilen
- 2;
578 case CI_COMPRESSTYPE
:
579 if (go
->neg_vj
|| no
.neg_vj
||
580 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
))
585 if ((go
->neg_addr
&& go
->old_addrs
) || no
.old_addrs
586 || cilen
!= CILEN_ADDRS
)
592 if (ciaddr1
&& go
->accept_local
)
593 try.ouraddr
= ciaddr1
;
596 if (ciaddr2
&& go
->accept_remote
)
597 try.hisaddr
= ciaddr2
;
601 if (go
->neg_addr
|| no
.neg_addr
|| cilen
!= CILEN_ADDR
)
606 if (ciaddr1
&& go
->accept_local
)
607 try.ouraddr
= ciaddr1
;
608 if (try.ouraddr
!= 0)
616 /* If there is still anything left, this packet is bad. */
621 * OK, the Nak is good. Now we can update state.
623 if (f
->state
!= OPENED
)
629 IPCPDEBUG((LOG_INFO
, "ipcp_nakci: received bad Nak!"));
635 * ipcp_rejci - Reject some of our CIs.
638 ipcp_rejci(fsm
*f
, u_char
*p
, int len
)
640 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
641 u_char cimaxslotindex
, ciflag
, cilen
;
644 ipcp_options
try; /* options to request next time */
648 * Any Rejected CIs must be in exactly the same order that we sent.
649 * Check packet length and CI length at each step.
650 * If we find any deviations, then this packet is bad.
652 #define REJCIADDR(opt, neg, old, val1, val2) \
654 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
662 /* Check rejected value. */ \
663 if (cilong != val1) \
668 /* Check rejected value. */ \
669 if (cilong != val2) \
675 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
677 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
682 GETSHORT(cishort, p); \
683 /* Check rejected value. */ \
684 if (cishort != val) \
687 GETCHAR(cimaxslotindex, p); \
688 if (cimaxslotindex != maxslot) \
690 GETCHAR(ciflag, p); \
691 if (ciflag != cflag) \
697 REJCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), neg_addr
,
698 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
700 REJCIVJ(CI_COMPRESSTYPE
, neg_vj
, go
->vj_protocol
, go
->old_vj
,
701 go
->maxslotindex
, go
->cflag
);
704 * If there are any remaining CIs, then this packet is bad.
709 * Now we can update state.
711 if (f
->state
!= OPENED
)
716 IPCPDEBUG((LOG_INFO
, "ipcp_rejci: received bad Reject!"));
722 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
724 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
725 * appropriately. If reject_if_disagree is non-zero, doesn't return
726 * CONFNAK; returns CONFREJ if it can't return CONFACK.
730 * len: Length of requested CIs
733 ipcp_reqci(fsm
*f
, u_char
*inp
, int *len
, int reject_if_disagree
)
735 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
736 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
737 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
738 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
739 u_char
*cip
, *next
; /* Pointer to current and next CIs */
740 u_short cilen
, citype
; /* Parsed len, type */
741 u_short cishort
; /* Parsed short value */
742 u_int32_t tl
, ciaddr1
, ciaddr2
;/* Parsed address values */
743 int rc
= CONFACK
; /* Final packet return code */
744 int orc
; /* Individual option return code */
745 u_char
*p
; /* Pointer to next char to parse */
746 u_char
*ucp
= inp
; /* Pointer to current output char */
747 int l
= *len
; /* Length left */
748 u_char maxslotindex
, cflag
;
751 cis_received
[f
->unit
] = 1;
754 * Reset all his options.
756 BZERO(ho
, sizeof(*ho
));
759 * Process all his options.
763 orc
= CONFACK
; /* Assume success */
764 cip
= p
= next
; /* Remember begining of CI */
765 if (l
< 2 || /* Not enough data for CI header or */
766 p
[1] < 2 || /* CI length too small or */
767 p
[1] > l
) { /* CI length too big? */
768 IPCPDEBUG((LOG_INFO
, "ipcp_reqci: bad CI length!"));
769 orc
= CONFREJ
; /* Reject bad CI */
770 cilen
= l
; /* Reject till end of packet */
771 l
= 0; /* Don't loop again */
774 GETCHAR(citype
, p
); /* Parse CI type */
775 GETCHAR(cilen
, p
); /* Parse CI length */
776 l
-= cilen
; /* Adjust remaining length */
777 next
+= cilen
; /* Step to next CI */
779 switch (citype
) { /* Check CI type */
781 IPCPDEBUG((LOG_INFO
, "ipcp: received ADDRS "));
783 cilen
!= CILEN_ADDRS
) { /* Check CI length */
784 orc
= CONFREJ
; /* Reject CI */
789 * If he has no address, or if we both have his address but
790 * disagree about it, then NAK it with our idea.
791 * In particular, if we don't know his address, but he does,
794 GETLONG(tl
, p
); /* Parse source address (his) */
796 IPCPDEBUG((LOG_INFO
, "(%s:", ip_ntoa(ciaddr1
)));
797 if (ciaddr1
!= wo
->hisaddr
798 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
800 if (!reject_if_disagree
) {
801 DECPTR(sizeof(u_int32_t
), p
);
802 tl
= ntohl(wo
->hisaddr
);
805 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
807 * If neither we nor he knows his address, reject the option.
810 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
815 * If he doesn't know our address, or if we both have our address
816 * but disagree about it, then NAK it with our idea.
818 GETLONG(tl
, p
); /* Parse desination address (ours) */
820 IPCPDEBUG((LOG_INFO
, "%s)", ip_ntoa(ciaddr2
)));
821 if (ciaddr2
!= wo
->ouraddr
) {
822 if (ciaddr2
== 0 || !wo
->accept_local
) {
824 if (!reject_if_disagree
) {
825 DECPTR(sizeof(u_int32_t
), p
);
826 tl
= ntohl(wo
->ouraddr
);
830 go
->ouraddr
= ciaddr2
; /* accept peer's idea */
836 ho
->hisaddr
= ciaddr1
;
837 ho
->ouraddr
= ciaddr2
;
841 IPCPDEBUG((LOG_INFO
, "ipcp: received ADDR "));
844 cilen
!= CILEN_ADDR
) { /* Check CI length */
845 orc
= CONFREJ
; /* Reject CI */
850 * If he has no address, or if we both have his address but
851 * disagree about it, then NAK it with our idea.
852 * In particular, if we don't know his address, but he does,
855 GETLONG(tl
, p
); /* Parse source address (his) */
857 IPCPDEBUG((LOG_INFO
, "(%s)", ip_ntoa(ciaddr1
)));
858 if (ciaddr1
!= wo
->hisaddr
859 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
861 if (!reject_if_disagree
) {
862 DECPTR(sizeof(u_int32_t
), p
);
863 tl
= ntohl(wo
->hisaddr
);
866 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
868 * Don't ACK an address of 0.0.0.0 - reject it instead.
871 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
876 ho
->hisaddr
= ciaddr1
;
881 /* Microsoft primary or secondary DNS request */
882 d
= citype
== CI_MS_DNS2
;
883 IPCPDEBUG((LOG_INFO
, "ipcp: received DNS%d Request ", d
+1));
885 /* If we do not have a DNS address then we cannot send it */
886 if (ao
->dnsaddr
[d
] == 0 ||
887 cilen
!= CILEN_ADDR
) { /* Check CI length */
888 orc
= CONFREJ
; /* Reject CI */
892 if (htonl(tl
) != ao
->dnsaddr
[d
]) {
893 DECPTR(sizeof(u_int32_t
), p
);
894 tl
= ntohl(ao
->dnsaddr
[d
]);
902 /* Microsoft primary or secondary WINS request */
903 d
= citype
== CI_MS_WINS2
;
904 IPCPDEBUG((LOG_INFO
, "ipcp: received WINS%d Request ", d
+1));
906 /* If we do not have a DNS address then we cannot send it */
907 if (ao
->winsaddr
[d
] == 0 ||
908 cilen
!= CILEN_ADDR
) { /* Check CI length */
909 orc
= CONFREJ
; /* Reject CI */
913 if (htonl(tl
) != ao
->winsaddr
[d
]) {
914 DECPTR(sizeof(u_int32_t
), p
);
915 tl
= ntohl(ao
->winsaddr
[d
]);
921 case CI_COMPRESSTYPE
:
922 IPCPDEBUG((LOG_INFO
, "ipcp: received COMPRESSTYPE "));
924 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
)) {
928 GETSHORT(cishort
, p
);
929 IPCPDEBUG((LOG_INFO
, "(%d)", cishort
));
931 if (!(cishort
== IPCP_VJ_COMP
||
932 (cishort
== IPCP_VJ_COMP_OLD
&& cilen
== CILEN_COMPRESS
))) {
938 ho
->vj_protocol
= cishort
;
939 if (cilen
== CILEN_VJ
) {
940 GETCHAR(maxslotindex
, p
);
941 if (maxslotindex
> ao
->maxslotindex
) {
943 if (!reject_if_disagree
){
945 PUTCHAR(ao
->maxslotindex
, p
);
949 if (cflag
&& !ao
->cflag
) {
951 if (!reject_if_disagree
){
953 PUTCHAR(wo
->cflag
, p
);
956 ho
->maxslotindex
= maxslotindex
;
960 ho
->maxslotindex
= MAX_STATES
- 1;
971 IPCPDEBUG((LOG_INFO
, " (%s)\n", CODENAME(orc
)));
973 if (orc
== CONFACK
&& /* Good CI */
974 rc
!= CONFACK
) /* but prior CI wasnt? */
975 continue; /* Don't send this one */
977 if (orc
== CONFNAK
) { /* Nak this CI? */
978 if (reject_if_disagree
) /* Getting fed up with sending NAKs? */
979 orc
= CONFREJ
; /* Get tough if so */
981 if (rc
== CONFREJ
) /* Rejecting prior CI? */
982 continue; /* Don't send this one */
983 if (rc
== CONFACK
) { /* Ack'd all prior CIs? */
984 rc
= CONFNAK
; /* Not anymore... */
985 ucp
= inp
; /* Backup */
990 if (orc
== CONFREJ
&& /* Reject this CI */
991 rc
!= CONFREJ
) { /* but no prior ones? */
993 ucp
= inp
; /* Backup */
996 /* Need to move CI? */
998 BCOPY(cip
, ucp
, cilen
); /* Move it */
1000 /* Update output pointer */
1005 * If we aren't rejecting this packet, and we want to negotiate
1006 * their address, and they didn't send their address, then we
1007 * send a NAK with a CI_ADDR option appended. We assume the
1008 * input buffer is long enough that we can append the extra
1011 if (rc
!= CONFREJ
&& !ho
->neg_addr
&&
1012 wo
->req_addr
&& !reject_if_disagree
) {
1013 if (rc
== CONFACK
) {
1015 ucp
= inp
; /* reset pointer */
1016 wo
->req_addr
= 0; /* don't ask again */
1018 PUTCHAR(CI_ADDR
, ucp
);
1019 PUTCHAR(CILEN_ADDR
, ucp
);
1020 tl
= ntohl(wo
->hisaddr
);
1024 *len
= ucp
- inp
; /* Compute output length */
1025 IPCPDEBUG((LOG_INFO
, "ipcp: returning Configure-%s", CODENAME(rc
)));
1026 return (rc
); /* Return final code */
1031 * ip_check_options - check that any IP-related options are OK,
1032 * and assign appropriate defaults.
1035 ip_check_options(void)
1039 ipcp_options
*wo
= &ipcp_wantoptions
[0];
1042 * Default our local IP address based on our hostname.
1043 * If local IP address already given, don't bother.
1045 if (wo
->ouraddr
== 0 && !disable_defaultip
) {
1047 * Look up our hostname (possibly with domain name appended)
1048 * and take the first IP address as our local IP address.
1049 * If there isn't an IP address for our hostname, too bad.
1051 wo
->accept_local
= 1; /* don't insist on this default value */
1052 if ((hp
= gethostbyname(hostname
)) != NULL
) {
1053 local
= *(u_int32_t
*)hp
->h_addr
;
1054 if (local
!= 0 && !bad_ip_adrs(local
))
1055 wo
->ouraddr
= local
;
1059 if (demand
&& wo
->hisaddr
== 0) {
1060 option_error("remote IP address required for demand-dialling\n");
1064 if (demand
&& wo
->accept_remote
) {
1065 option_error("ipcp-accept-remote is incompatible with demand\n");
1073 * ip_demand_conf - configure the interface as though
1074 * IPCP were up, for use with dial-on-demand.
1077 ip_demand_conf(int u
)
1079 ipcp_options
*wo
= &ipcp_wantoptions
[u
];
1081 if (!sifaddr(u
, wo
->ouraddr
, wo
->hisaddr
, GetMask(wo
->ouraddr
)))
1085 if (!sifnpmode(u
, PPP_IP
, NPMODE_QUEUE
))
1087 if (wo
->default_route
)
1088 if (sifdefaultroute(u
, wo
->ouraddr
, wo
->hisaddr
))
1089 default_route_set
[u
] = 1;
1091 if (sifproxyarp(u
, wo
->hisaddr
))
1092 proxy_arp_set
[u
] = 1;
1094 syslog(LOG_NOTICE
, "local IP address %s", ip_ntoa(wo
->ouraddr
));
1095 syslog(LOG_NOTICE
, "remote IP address %s", ip_ntoa(wo
->hisaddr
));
1102 * ipcp_up - IPCP has come UP.
1104 * Configure the IP network interface appropriately and bring it up.
1110 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1111 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1112 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1114 np_up(f
->unit
, PPP_IP
);
1115 IPCPDEBUG((LOG_INFO
, "ipcp: up"));
1118 * We must have a non-zero IP address for both ends of the link.
1121 ho
->hisaddr
= wo
->hisaddr
;
1123 if (ho
->hisaddr
== 0) {
1124 syslog(LOG_ERR
, "Could not determine remote IP address");
1125 ipcp_close(f
->unit
, "Could not determine remote IP address");
1128 if (go
->ouraddr
== 0) {
1129 syslog(LOG_ERR
, "Could not determine local IP address");
1130 ipcp_close(f
->unit
, "Could not determine local IP address");
1133 script_setenv("IPLOCAL", ip_ntoa(go
->ouraddr
));
1134 script_setenv("IPREMOTE", ip_ntoa(ho
->hisaddr
));
1137 * Check that the peer is allowed to use the IP address it wants.
1139 if (!auth_ip_addr(f
->unit
, ho
->hisaddr
)) {
1140 syslog(LOG_ERR
, "Peer is not authorized to use remote address %s",
1141 ip_ntoa(ho
->hisaddr
));
1142 ipcp_close(f
->unit
, "Unauthorized remote IP address");
1146 /* set tcp compression */
1147 sifvjcomp(f
->unit
, ho
->neg_vj
, ho
->cflag
, ho
->maxslotindex
);
1150 * If we are doing dial-on-demand, the interface is already
1151 * configured, so we put out any saved-up packets, then set the
1152 * interface to pass IP packets.
1155 if (go
->ouraddr
!= wo
->ouraddr
|| ho
->hisaddr
!= wo
->hisaddr
) {
1156 if (go
->ouraddr
!= wo
->ouraddr
)
1157 syslog(LOG_WARNING
, "Local IP address changed to %s",
1158 ip_ntoa(go
->ouraddr
));
1159 if (ho
->hisaddr
!= wo
->hisaddr
)
1160 syslog(LOG_WARNING
, "Remote IP address changed to %s",
1161 ip_ntoa(ho
->hisaddr
));
1162 ipcp_clear_addrs(f
->unit
);
1164 /* Set the interface to the new addresses */
1165 mask
= GetMask(go
->ouraddr
);
1166 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1167 IPCPDEBUG((LOG_WARNING
, "sifaddr failed"));
1168 ipcp_close(f
->unit
, "Interface configuration failed");
1172 /* assign a default route through the interface if required */
1173 if (ipcp_wantoptions
[f
->unit
].default_route
)
1174 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1175 default_route_set
[f
->unit
] = 1;
1177 /* Make a proxy ARP entry if requested. */
1178 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1179 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1180 proxy_arp_set
[f
->unit
] = 1;
1183 demand_rexmit(PPP_IP
);
1184 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1188 * Set IP addresses and (if specified) netmask.
1190 mask
= GetMask(go
->ouraddr
);
1192 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1193 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1194 IPCPDEBUG((LOG_WARNING
, "sifaddr failed"));
1195 ipcp_close(f
->unit
, "Interface configuration failed");
1200 /* bring the interface up for IP */
1201 if (!sifup(f
->unit
)) {
1202 IPCPDEBUG((LOG_WARNING
, "sifup failed"));
1203 ipcp_close(f
->unit
, "Interface configuration failed");
1207 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1208 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1209 IPCPDEBUG((LOG_WARNING
, "sifaddr failed"));
1210 ipcp_close(f
->unit
, "Interface configuration failed");
1214 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1216 /* assign a default route through the interface if required */
1217 if (ipcp_wantoptions
[f
->unit
].default_route
)
1218 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1219 default_route_set
[f
->unit
] = 1;
1221 /* Make a proxy ARP entry if requested. */
1222 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1223 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1224 proxy_arp_set
[f
->unit
] = 1;
1226 syslog(LOG_NOTICE
, "local IP address %s", ip_ntoa(go
->ouraddr
));
1227 syslog(LOG_NOTICE
, "remote IP address %s", ip_ntoa(ho
->hisaddr
));
1231 * Execute the ip-up script, like this:
1232 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1234 ipcp_script(f
, _PATH_IPUP
);
1240 * ipcp_down - IPCP has gone DOWN.
1242 * Take the IP network interface down, clear its addresses
1243 * and delete routes through it.
1248 IPCPDEBUG((LOG_INFO
, "ipcp: down"));
1249 np_down(f
->unit
, PPP_IP
);
1250 sifvjcomp(f
->unit
, 0, 0, 0);
1253 * If we are doing dial-on-demand, set the interface
1254 * to queue up outgoing packets (for now).
1257 sifnpmode(f
->unit
, PPP_IP
, NPMODE_QUEUE
);
1260 ipcp_clear_addrs(f
->unit
);
1263 /* Execute the ip-down script */
1264 ipcp_script(f
, _PATH_IPDOWN
);
1269 * ipcp_clear_addrs() - clear the interface addresses, routes,
1270 * proxy arp entries, etc.
1273 ipcp_clear_addrs(int unit
)
1275 u_int32_t ouraddr
, hisaddr
;
1277 ouraddr
= ipcp_gotoptions
[unit
].ouraddr
;
1278 hisaddr
= ipcp_hisoptions
[unit
].hisaddr
;
1279 if (proxy_arp_set
[unit
]) {
1280 cifproxyarp(unit
, hisaddr
);
1281 proxy_arp_set
[unit
] = 0;
1283 if (default_route_set
[unit
]) {
1284 cifdefaultroute(unit
, ouraddr
, hisaddr
);
1285 default_route_set
[unit
] = 0;
1287 cifaddr(unit
, ouraddr
, hisaddr
);
1292 * ipcp_finished - possibly shut down the lower layers.
1295 ipcp_finished(fsm
*f
)
1297 np_finished(f
->unit
, PPP_IP
);
1302 * ipcp_script - Execute a script with arguments
1303 * interface-name tty-name speed local-IP remote-IP.
1306 ipcp_script(fsm
*f
, char *script
)
1308 char strspeed
[32], strlocal
[32], strremote
[32];
1311 sprintf(strspeed
, "%d", baud_rate
);
1312 strcpy(strlocal
, ip_ntoa(ipcp_gotoptions
[f
->unit
].ouraddr
));
1313 strcpy(strremote
, ip_ntoa(ipcp_hisoptions
[f
->unit
].hisaddr
));
1320 argv
[5] = strremote
;
1323 run_program(script
, argv
, 0);
1327 * ipcp_printpkt - print the contents of an IPCP packet.
1329 static char *ipcp_codenames
[] = {
1330 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1331 "TermReq", "TermAck", "CodeRej"
1335 ipcp_printpkt(u_char
*p
, int plen
, void (*printer
)(void *, char *, ...),
1338 int code
, id
, len
, olen
;
1339 u_char
*pstart
, *optend
;
1343 if (plen
< HEADERLEN
)
1349 if (len
< HEADERLEN
|| len
> plen
)
1352 if (code
>= 1 && code
<= sizeof(ipcp_codenames
) / sizeof(char *))
1353 printer(arg
, " %s", ipcp_codenames
[code
-1]);
1355 printer(arg
, " code=0x%x", code
);
1356 printer(arg
, " id=0x%x", id
);
1363 /* print option list */
1368 if (olen
< 2 || olen
> len
) {
1376 if (olen
== CILEN_ADDRS
) {
1379 printer(arg
, "addrs %I", htonl(cilong
));
1381 printer(arg
, " %I", htonl(cilong
));
1384 case CI_COMPRESSTYPE
:
1385 if (olen
>= CILEN_COMPRESS
) {
1387 GETSHORT(cishort
, p
);
1388 printer(arg
, "compress ");
1393 case IPCP_VJ_COMP_OLD
:
1394 printer(arg
, "old-VJ");
1397 printer(arg
, "0x%x", cishort
);
1402 if (olen
== CILEN_ADDR
) {
1405 printer(arg
, "addr %I", htonl(cilong
));
1412 printer(arg
, "ms-dns %I", htonl(cilong
));
1418 printer(arg
, "ms-wins %I", htonl(cilong
));
1421 while (p
< optend
) {
1423 printer(arg
, " %.2x", code
);
1431 if (len
> 0 && *p
>= ' ' && *p
< 0x7f) {
1433 print_string(p
, len
, printer
, arg
);
1440 /* print the rest of the bytes in the packet */
1441 for (; len
> 0; --len
) {
1443 printer(arg
, " %.2x", code
);
1450 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1451 * We don't bring the link up for IP fragments or for TCP FIN packets
1454 #define IP_HDRLEN 20 /* bytes */
1455 #define IP_OFFMASK 0x1fff
1456 #define IPPROTO_TCP 6
1457 #define TCP_HDRLEN 20
1461 * We use these macros because the IP header may be at an odd address,
1462 * and some compilers might use word loads to get th_off or ip_hl.
1465 #define net_short(x) (((x)[0] << 8) + (x)[1])
1466 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1467 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1468 #define get_ipproto(x) (((unsigned char *)(x))[9])
1469 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1470 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1473 ip_active_pkt(u_char
*pkt
, int len
)
1480 if (len
< IP_HDRLEN
)
1482 if ((get_ipoff(pkt
) & IP_OFFMASK
) != 0)
1484 if (get_ipproto(pkt
) != IPPROTO_TCP
)
1486 hlen
= get_iphl(pkt
) * 4;
1487 if (len
< hlen
+ TCP_HDRLEN
)
1490 if ((get_tcpflags(tcp
) & TH_FIN
) != 0 && len
== hlen
+ get_tcpoff(tcp
) * 4)