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.
20 #define RCSID "$Id: ipcp.c,v 1.2 2004/08/12 02:56:55 tallest Exp $"
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #ifdef UNNUMBERIP_SUPPORT
34 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #include "pathnames.h"
47 static const char rcsid
[] = RCSID
;
50 ipcp_options ipcp_wantoptions
[NUM_PPP
]; /* Options that we want to request */
51 ipcp_options ipcp_gotoptions
[NUM_PPP
]; /* Options that peer ack'd */
52 ipcp_options ipcp_allowoptions
[NUM_PPP
]; /* Options we allow peer to request */
53 ipcp_options ipcp_hisoptions
[NUM_PPP
]; /* Options that we ack'd */
55 u_int32_t netmask
= 0; /* IP netmask to set on interface */
57 //bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
58 bool disable_defaultip
= 1; /* Don't use hostname for default IP adrs */
60 /* Hook for a plugin to know when IP protocol has come up */
61 void (*ip_up_hook
) __P((void)) = NULL
;
63 /* Hook for a plugin to know when IP protocol has come down */
64 void (*ip_down_hook
) __P((void)) = NULL
;
66 /* Hook for a plugin to choose the remote IP address */
67 void (*ip_choose_hook
) __P((u_int32_t
*)) = NULL
;
70 static int default_route_set
[NUM_PPP
]; /* Have set up a default route */
71 static int proxy_arp_set
[NUM_PPP
]; /* Have created proxy arp entry */
72 static bool usepeerdns
; /* Ask peer for DNS addrs */
73 static int ipcp_is_up
; /* have called np_up() */
74 static bool ask_for_local
; /* request our address from peer */
75 static char vj_value
[8]; /* string form of vj option value */
76 static char netmask_str
[20]; /* string form of netmask value */
79 * Callbacks for fsm code. (CI = Configuration Information)
81 static void ipcp_resetci
__P((fsm
*)); /* Reset our CI */
82 static int ipcp_cilen
__P((fsm
*)); /* Return length of our CI */
83 static void ipcp_addci
__P((fsm
*, u_char
*, int *)); /* Add our CI */
84 static int ipcp_ackci
__P((fsm
*, u_char
*, int)); /* Peer ack'd our CI */
85 static int ipcp_nakci
__P((fsm
*, u_char
*, int)); /* Peer nak'd our CI */
86 static int ipcp_rejci
__P((fsm
*, u_char
*, int)); /* Peer rej'd our CI */
87 static int ipcp_reqci
__P((fsm
*, u_char
*, int *, int)); /* Rcv CI */
88 static void ipcp_up
__P((fsm
*)); /* We're UP */
89 static void ipcp_down
__P((fsm
*)); /* We're DOWN */
90 static void ipcp_finished
__P((fsm
*)); /* Don't need lower layer */
92 fsm ipcp_fsm
[NUM_PPP
]; /* IPCP fsm structure */
94 static fsm_callbacks ipcp_callbacks
= { /* IPCP callback routines */
95 ipcp_resetci
, /* Reset our Configuration Information */
96 ipcp_cilen
, /* Length of our Configuration Information */
97 ipcp_addci
, /* Add our Configuration Information */
98 ipcp_ackci
, /* ACK our Configuration Information */
99 ipcp_nakci
, /* NAK our Configuration Information */
100 ipcp_rejci
, /* Reject our Configuration Information */
101 ipcp_reqci
, /* Request peer's Configuration Information */
102 ipcp_up
, /* Called when fsm reaches OPENED state */
103 ipcp_down
, /* Called when fsm leaves OPENED state */
104 NULL
, /* Called when we want the lower layer up */
105 ipcp_finished
, /* Called when we want the lower layer down */
106 NULL
, /* Called when Protocol-Reject received */
107 NULL
, /* Retransmission is necessary */
108 NULL
, /* Called to handle protocol-specific codes */
109 "IPCP" /* String name of protocol */
113 * Command-line options.
115 static int setvjslots
__P((char **));
116 static int setdnsaddr
__P((char **));
117 static int setwinsaddr
__P((char **));
118 static int setnetmask
__P((char **));
119 static int setipaddr
__P((char *, char **, int));
120 static void printipaddr
__P((option_t
*, void (*)(void *, char *,...),void *));
122 static option_t ipcp_option_list
[] = {
123 { "noip", o_bool
, &ipcp_protent
.enabled_flag
,
124 "Disable IP and IPCP" },
125 { "-ip", o_bool
, &ipcp_protent
.enabled_flag
,
126 "Disable IP and IPCP", OPT_ALIAS
},
128 { "novj", o_bool
, &ipcp_wantoptions
[0].neg_vj
,
129 "Disable VJ compression", OPT_A2CLR
, &ipcp_allowoptions
[0].neg_vj
},
130 { "-vj", o_bool
, &ipcp_wantoptions
[0].neg_vj
,
131 "Disable VJ compression", OPT_ALIAS
| OPT_A2CLR
,
132 &ipcp_allowoptions
[0].neg_vj
},
134 { "novjccomp", o_bool
, &ipcp_wantoptions
[0].cflag
,
135 "Disable VJ connection-ID compression", OPT_A2CLR
,
136 &ipcp_allowoptions
[0].cflag
},
137 { "-vjccomp", o_bool
, &ipcp_wantoptions
[0].cflag
,
138 "Disable VJ connection-ID compression", OPT_ALIAS
| OPT_A2CLR
,
139 &ipcp_allowoptions
[0].cflag
},
141 { "vj-max-slots", o_special
, (void *)setvjslots
,
142 "Set maximum VJ header slots",
143 OPT_PRIO
| OPT_A2STRVAL
| OPT_STATIC
, vj_value
},
145 { "ipcp-accept-local", o_bool
, &ipcp_wantoptions
[0].accept_local
,
146 "Accept peer's address for us", 1 },
147 { "ipcp-accept-remote", o_bool
, &ipcp_wantoptions
[0].accept_remote
,
148 "Accept peer's address for it", 1 },
150 { "ipparam", o_string
, &ipparam
,
151 "Set ip script parameter", OPT_PRIO
},
153 { "noipdefault", o_bool
, &disable_defaultip
,
154 "Don't use name for default IP adrs", 1 },
156 { "ms-dns", 1, (void *)setdnsaddr
,
157 "DNS address for the peer's use" },
158 { "ms-wins", 1, (void *)setwinsaddr
,
159 "Nameserver for SMB over TCP/IP for peer" },
161 { "ipcp-restart", o_int
, &ipcp_fsm
[0].timeouttime
,
162 "Set timeout for IPCP", OPT_PRIO
},
163 { "ipcp-max-terminate", o_int
, &ipcp_fsm
[0].maxtermtransmits
,
164 "Set max #xmits for term-reqs", OPT_PRIO
},
165 { "ipcp-max-configure", o_int
, &ipcp_fsm
[0].maxconfreqtransmits
,
166 "Set max #xmits for conf-reqs", OPT_PRIO
},
167 { "ipcp-max-failure", o_int
, &ipcp_fsm
[0].maxnakloops
,
168 "Set max #conf-naks for IPCP", OPT_PRIO
},
170 { "defaultroute", o_bool
, &ipcp_wantoptions
[0].default_route
,
171 "Add default route", OPT_ENABLE
|1, &ipcp_allowoptions
[0].default_route
},
172 { "nodefaultroute", o_bool
, &ipcp_allowoptions
[0].default_route
,
173 "disable defaultroute option", OPT_A2CLR
,
174 &ipcp_wantoptions
[0].default_route
},
175 { "-defaultroute", o_bool
, &ipcp_allowoptions
[0].default_route
,
176 "disable defaultroute option", OPT_ALIAS
| OPT_A2CLR
,
177 &ipcp_wantoptions
[0].default_route
},
179 { "proxyarp", o_bool
, &ipcp_wantoptions
[0].proxy_arp
,
180 "Add proxy ARP entry", OPT_ENABLE
|1, &ipcp_allowoptions
[0].proxy_arp
},
181 { "noproxyarp", o_bool
, &ipcp_allowoptions
[0].proxy_arp
,
182 "disable proxyarp option", OPT_A2CLR
,
183 &ipcp_wantoptions
[0].proxy_arp
},
184 { "-proxyarp", o_bool
, &ipcp_allowoptions
[0].proxy_arp
,
185 "disable proxyarp option", OPT_ALIAS
| OPT_A2CLR
,
186 &ipcp_wantoptions
[0].proxy_arp
},
188 { "usepeerdns", o_bool
, &usepeerdns
,
189 "Ask peer for DNS address(es)", 1 },
191 { "netmask", o_special
, (void *)setnetmask
,
192 "set netmask", OPT_PRIO
| OPT_A2STRVAL
| OPT_STATIC
, netmask_str
},
194 { "IP addresses", o_wild
, (void *) &setipaddr
,
195 "set local and remote IP addresses",
196 OPT_NOARG
| OPT_A2PRINTER
, (void *) &printipaddr
},
202 * Protocol entry points from main code.
204 static void ipcp_init
__P((int));
205 static void ipcp_open
__P((int));
206 static void ipcp_close
__P((int, char *));
207 static void ipcp_lowerup
__P((int));
208 static void ipcp_lowerdown
__P((int));
209 static void ipcp_input
__P((int, u_char
*, int));
210 static void ipcp_protrej
__P((int));
211 static int ipcp_printpkt
__P((u_char
*, int,
212 void (*) __P((void *, char *, ...)), void *));
213 static void ip_check_options
__P((void));
214 static int ip_demand_conf
__P((int));
215 static int ip_active_pkt
__P((u_char
*, int));
216 static void create_resolv
__P((u_int32_t
, u_int32_t
));
218 struct protent ipcp_protent
= {
238 static void ipcp_clear_addrs
__P((int, u_int32_t
, u_int32_t
));
239 static void ipcp_script
__P((char *)); /* Run an up/down script */
240 static void ipcp_script_done
__P((void *));
242 #ifdef UNNUMBERIP_SUPPORT
244 * get_lan_ip - get LAN IP for unnumber ip use.
246 static unsigned int get_lan_ip(void); // tallest 0129
250 * Lengths of configuration options.
253 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
254 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
255 #define CILEN_ADDR 6 /* new-style single address option */
256 #define CILEN_ADDRS 10 /* old-style dual address option */
259 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
260 (x) == CONFNAK ? "NAK" : "REJ")
263 * This state variable is used to ensure that we don't
264 * run an ipcp-up/down script while one is already running.
266 static enum script_state
{
270 static pid_t ipcp_script_pid
;
273 * Make a string representation of a network IP address.
281 slprintf(b
, sizeof(b
), "%I", ipaddr
);
290 * setvjslots - set maximum number of connection slots for VJ compression
298 if (!int_option(*argv
, &value
))
300 if (value
< 2 || value
> 16) {
301 option_error("vj-max-slots value must be between 2 and 16");
304 ipcp_wantoptions
[0].maxslotindex
=
305 ipcp_allowoptions
[0].maxslotindex
= value
- 1;
306 slprintf(vj_value
, sizeof(vj_value
), "%d", value
);
311 * setdnsaddr - set the dns address(es)
320 dns
= inet_addr(*argv
);
321 if (dns
== (u_int32_t
) -1) {
322 if ((hp
= gethostbyname(*argv
)) == NULL
) {
323 option_error("invalid address parameter '%s' for ms-dns option",
327 dns
= *(u_int32_t
*)hp
->h_addr
;
330 /* We take the last 2 values given, the 2nd-last as the primary
331 and the last as the secondary. If only one is given it
332 becomes both primary and secondary. */
333 if (ipcp_allowoptions
[0].dnsaddr
[1] == 0)
334 ipcp_allowoptions
[0].dnsaddr
[0] = dns
;
336 ipcp_allowoptions
[0].dnsaddr
[0] = ipcp_allowoptions
[0].dnsaddr
[1];
338 /* always set the secondary address value. */
339 ipcp_allowoptions
[0].dnsaddr
[1] = dns
;
345 * setwinsaddr - set the wins address(es)
346 * This is primrarly used with the Samba package under UNIX or for pointing
347 * the caller to the existing WINS server on a Windows NT platform.
356 wins
= inet_addr(*argv
);
357 if (wins
== (u_int32_t
) -1) {
358 if ((hp
= gethostbyname(*argv
)) == NULL
) {
359 option_error("invalid address parameter '%s' for ms-wins option",
363 wins
= *(u_int32_t
*)hp
->h_addr
;
366 /* We take the last 2 values given, the 2nd-last as the primary
367 and the last as the secondary. If only one is given it
368 becomes both primary and secondary. */
369 if (ipcp_allowoptions
[0].winsaddr
[1] == 0)
370 ipcp_allowoptions
[0].winsaddr
[0] = wins
;
372 ipcp_allowoptions
[0].winsaddr
[0] = ipcp_allowoptions
[0].winsaddr
[1];
374 /* always set the secondary address value. */
375 ipcp_allowoptions
[0].winsaddr
[1] = wins
;
381 * setipaddr - Set the IP address
382 * If doit is 0, the call is to check whether this option is
383 * potentially an IP address specification.
386 setipaddr(arg
, argv
, doit
)
393 u_int32_t local
, remote
;
394 ipcp_options
*wo
= &ipcp_wantoptions
[0];
395 static int prio_local
= 0, prio_remote
= 0;
398 * IP address pair separated by ":".
400 if ((colon
= strchr(arg
, ':')) == NULL
)
406 * If colon first character, then no local addr.
408 if (colon
!= arg
&& option_priority
>= prio_local
) {
410 if ((local
= inet_addr(arg
)) == (u_int32_t
) -1) {
411 if ((hp
= gethostbyname(arg
)) == NULL
) {
413 // LOGX_ERROR("Unknown host: %s", arg);
414 option_error("unknown host: %s", arg
);
417 local
= *(u_int32_t
*)hp
->h_addr
;
419 if (bad_ip_adrs(local
)) {
420 LOGX_ERROR("Invalid local IP address %s.", ip_ntoa(local
));
421 option_error("bad local IP address %s", ip_ntoa(local
));
427 prio_local
= option_priority
;
431 * If colon last character, then no remote addr.
433 if (*++colon
!= '\0' && option_priority
>= prio_remote
) {
434 if ((remote
= inet_addr(colon
)) == (u_int32_t
) -1) {
435 if ((hp
= gethostbyname(colon
)) == NULL
) {
436 // LOGX_ERROR("Unknown host: %s", colon);
437 option_error("unknown host: %s", colon
);
440 remote
= *(u_int32_t
*)hp
->h_addr
;
441 if (remote_name
[0] == 0)
442 strlcpy(remote_name
, colon
, sizeof(remote_name
));
444 if (bad_ip_adrs(remote
)) {
445 LOGX_ERROR("Invalid remote IP address %s.", ip_ntoa(remote
));
446 option_error("bad remote IP address %s", ip_ntoa(remote
));
450 wo
->hisaddr
= remote
;
451 prio_remote
= option_priority
;
458 printipaddr(opt
, printer
, arg
)
460 void (*printer
) __P((void *, char *, ...));
463 ipcp_options
*wo
= &ipcp_wantoptions
[0];
465 if (wo
->ouraddr
!= 0)
466 printer(arg
, "%I", wo
->ouraddr
);
468 if (wo
->hisaddr
!= 0)
469 printer(arg
, "%I", wo
->hisaddr
);
473 * setnetmask - set the netmask to be used on the interface.
484 * Unfortunately, if we use inet_addr, we can't tell whether
485 * a result of all 1s is an error or a valid 255.255.255.255.
488 n
= parse_dotted_ip(p
, &mask
);
492 if (n
== 0 || p
[n
] != 0 || (netmask
& ~mask
) != 0) {
493 LOGX_ERROR("Invalid netmask value '%s'.", *argv
);
494 option_error("invalid netmask value '%s'", *argv
);
499 slprintf(netmask_str
, sizeof(netmask_str
), "%I", mask
);
505 parse_dotted_ip(p
, vp
)
515 b
= strtoul(p
, &endp
, 0);
521 /* accept e.g. 0xffffff00 */
539 * ipcp_init - Initialize IPCP.
545 fsm
*f
= &ipcp_fsm
[unit
];
546 ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
547 ipcp_options
*ao
= &ipcp_allowoptions
[unit
];
550 f
->protocol
= PPP_IPCP
;
551 f
->callbacks
= &ipcp_callbacks
;
552 fsm_init(&ipcp_fsm
[unit
]);
554 memset(wo
, 0, sizeof(*wo
));
555 memset(ao
, 0, sizeof(*ao
));
559 wo
->vj_protocol
= IPCP_VJ_COMP
;
560 wo
->maxslotindex
= MAX_STATES
- 1; /* really max index */
564 /* max slots and slot-id compression are currently hardwired in */
565 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
570 ao
->maxslotindex
= MAX_STATES
- 1;
574 ao
->default_route
= 1;
579 * ipcp_open - IPCP is allowed to come up.
585 fsm_open(&ipcp_fsm
[unit
]);
590 * ipcp_close - Take IPCP down.
593 ipcp_close(unit
, reason
)
597 fsm_close(&ipcp_fsm
[unit
], reason
);
602 * ipcp_lowerup - The lower layer is up.
608 fsm_lowerup(&ipcp_fsm
[unit
]);
613 * ipcp_lowerdown - The lower layer is down.
619 fsm_lowerdown(&ipcp_fsm
[unit
]);
624 * ipcp_input - Input IPCP packet.
627 ipcp_input(unit
, p
, len
)
632 fsm_input(&ipcp_fsm
[unit
], p
, len
);
637 * ipcp_protrej - A Protocol-Reject was received for IPCP.
639 * Pretend the lower layer went down, so we shut up.
645 fsm_lowerdown(&ipcp_fsm
[unit
]);
650 * ipcp_resetci - Reset our CI.
651 * Called by fsm_sconfreq, Send Configure Request.
657 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
658 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
660 wo
->req_addr
= wo
->neg_addr
&& ipcp_allowoptions
[f
->unit
].neg_addr
;
661 if (wo
->ouraddr
== 0)
662 wo
->accept_local
= 1;
663 if (wo
->hisaddr
== 0)
664 wo
->accept_remote
= 1;
665 wo
->req_dns1
= usepeerdns
; /* Request DNS addresses from the peer */
666 wo
->req_dns2
= usepeerdns
;
671 ip_choose_hook(&wo
->hisaddr
);
676 * ipcp_cilen - Return length of our CI.
677 * Called by fsm_sconfreq, Send Configure Request.
683 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
684 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
685 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
687 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
688 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
689 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
692 * First see if we want to change our options to the old
693 * forms because we have received old forms from the peer.
695 if (wo
->neg_addr
&& !go
->neg_addr
&& !go
->old_addrs
) {
696 /* use the old style of address negotiation */
700 if (wo
->neg_vj
&& !go
->neg_vj
&& !go
->old_vj
) {
701 /* try an older style of VJ negotiation */
702 /* use the old style only if the peer did */
703 if (ho
->neg_vj
&& ho
->old_vj
) {
706 go
->vj_protocol
= ho
->vj_protocol
;
710 return (LENCIADDR(go
->neg_addr
, go
->old_addrs
) +
711 LENCIVJ(go
->neg_vj
, go
->old_vj
) +
712 LENCIDNS(go
->req_dns1
) +
713 LENCIDNS(go
->req_dns2
)) ;
718 * ipcp_addci - Add our desired CIs to a packet.
719 * Called by fsm_sconfreq, Send Configure Request.
722 ipcp_addci(f
, ucp
, lenp
)
727 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
730 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
732 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
733 if (len >= vjlen) { \
735 PUTCHAR(vjlen, ucp); \
736 PUTSHORT(val, ucp); \
738 PUTCHAR(maxslotindex, ucp); \
739 PUTCHAR(cflag, ucp); \
746 #define ADDCIADDR(opt, neg, old, val1, val2) \
748 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
749 if (len >= addrlen) { \
752 PUTCHAR(addrlen, ucp); \
764 #define ADDCIDNS(opt, neg, addr) \
766 if (len >= CILEN_ADDR) { \
769 PUTCHAR(CILEN_ADDR, ucp); \
777 ADDCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), go
->neg_addr
,
778 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
780 ADDCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
781 go
->maxslotindex
, go
->cflag
);
783 ADDCIDNS(CI_MS_DNS1
, go
->req_dns1
, go
->dnsaddr
[0]);
785 ADDCIDNS(CI_MS_DNS2
, go
->req_dns2
, go
->dnsaddr
[1]);
792 * ipcp_ackci - Ack our CIs.
793 * Called by fsm_rconfack, Receive Configure ACK.
800 ipcp_ackci(f
, p
, len
)
805 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
806 u_short cilen
, citype
, cishort
;
808 u_char cimaxslotindex
, cicflag
;
811 * CIs must be in exactly the same order that we sent...
812 * Check packet length and CI length at each step.
813 * If we find any deviations, then this packet is bad.
816 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
818 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
819 if ((len -= vjlen) < 0) \
821 GETCHAR(citype, p); \
823 if (cilen != vjlen || \
826 GETSHORT(cishort, p); \
827 if (cishort != val) \
830 GETCHAR(cimaxslotindex, p); \
831 if (cimaxslotindex != maxslotindex) \
833 GETCHAR(cicflag, p); \
834 if (cicflag != cflag) \
839 #define ACKCIADDR(opt, neg, old, val1, val2) \
841 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
843 if ((len -= addrlen) < 0) \
845 GETCHAR(citype, p); \
847 if (cilen != addrlen || \
852 if (val1 != cilong) \
857 if (val2 != cilong) \
862 #define ACKCIDNS(opt, neg, addr) \
865 if ((len -= CILEN_ADDR) < 0) \
867 GETCHAR(citype, p); \
869 if (cilen != CILEN_ADDR || citype != opt) \
873 if (addr != cilong) \
877 ACKCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), go
->neg_addr
,
878 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
880 ACKCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
881 go
->maxslotindex
, go
->cflag
);
883 ACKCIDNS(CI_MS_DNS1
, go
->req_dns1
, go
->dnsaddr
[0]);
885 ACKCIDNS(CI_MS_DNS2
, go
->req_dns2
, go
->dnsaddr
[1]);
888 * If there are any remaining CIs, then this packet is bad.
895 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
900 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
901 * This should not modify any state if the Nak is bad
902 * or if IPCP is in the OPENED state.
903 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
910 ipcp_nakci(f
, p
, len
)
915 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
916 u_char cimaxslotindex
, cicflag
;
917 u_char citype
, cilen
, *next
;
919 u_int32_t ciaddr1
, ciaddr2
, l
, cidnsaddr
;
920 ipcp_options no
; /* options we've seen Naks for */
921 ipcp_options
try; /* options to request next time */
923 BZERO(&no
, sizeof(no
));
927 * Any Nak'd CIs must be in exactly the same order that we sent.
928 * Check packet length and CI length at each step.
929 * If we find any deviations, then this packet is bad.
931 #define NAKCIADDR(opt, neg, old, code) \
933 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
939 ciaddr1 = htonl(l); \
942 ciaddr2 = htonl(l); \
950 #define NAKCIVJ(opt, neg, code) \
952 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
957 GETSHORT(cishort, p); \
962 #define NAKCIDNS(opt, neg, code) \
964 ((cilen = p[1]) == CILEN_ADDR) && \
970 cidnsaddr = htonl(l); \
976 * Accept the peer's idea of {our,his} address, if different
977 * from our idea, only if the accept_{local,remote} flag is set.
979 NAKCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), neg_addr
, go
->old_addrs
,
980 if (go
->accept_local
&& ciaddr1
) { /* Do we know our address? */
981 try.ouraddr
= ciaddr1
;
983 if (go
->accept_remote
&& ciaddr2
) { /* Does he know his? */
984 try.hisaddr
= ciaddr2
;
989 * Accept the peer's value of maxslotindex provided that it
990 * is less than what we asked for. Turn off slot-ID compression
991 * if the peer wants. Send old-style compress-type option if
994 NAKCIVJ(CI_COMPRESSTYPE
, neg_vj
,
995 if (cilen
== CILEN_VJ
) {
996 GETCHAR(cimaxslotindex
, p
);
998 if (cishort
== IPCP_VJ_COMP
) {
1000 if (cimaxslotindex
< go
->maxslotindex
)
1001 try.maxslotindex
= cimaxslotindex
;
1008 if (cishort
== IPCP_VJ_COMP
|| cishort
== IPCP_VJ_COMP_OLD
) {
1010 try.vj_protocol
= cishort
;
1017 NAKCIDNS(CI_MS_DNS1
, req_dns1
,
1018 try.dnsaddr
[0] = cidnsaddr
;
1021 NAKCIDNS(CI_MS_DNS2
, req_dns2
,
1022 try.dnsaddr
[1] = cidnsaddr
;
1026 * There may be remaining CIs, if the peer is requesting negotiation
1027 * on an option that we didn't include in our request packet.
1028 * If they want to negotiate about IP addresses, we comply.
1029 * If they want us to ask for compression, we refuse.
1031 while (len
> CILEN_VOID
) {
1034 if( (len
-= cilen
) < 0 )
1036 next
= p
+ cilen
- 2;
1039 case CI_COMPRESSTYPE
:
1040 if (go
->neg_vj
|| no
.neg_vj
||
1041 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
))
1046 if ((go
->neg_addr
&& go
->old_addrs
) || no
.old_addrs
1047 || cilen
!= CILEN_ADDRS
)
1053 if (ciaddr1
&& go
->accept_local
)
1054 try.ouraddr
= ciaddr1
;
1057 if (ciaddr2
&& go
->accept_remote
)
1058 try.hisaddr
= ciaddr2
;
1062 if (go
->neg_addr
|| no
.neg_addr
|| cilen
!= CILEN_ADDR
)
1067 if (ciaddr1
&& go
->accept_local
)
1068 try.ouraddr
= ciaddr1
;
1069 if (try.ouraddr
!= 0)
1078 * OK, the Nak is good. Now we can update state.
1079 * If there are any remaining options, we ignore them.
1081 if (f
->state
!= OPENED
)
1087 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1093 * ipcp_rejci - Reject some of our CIs.
1094 * Callback from fsm_rconfnakrej.
1097 ipcp_rejci(f
, p
, len
)
1102 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1103 u_char cimaxslotindex
, ciflag
, cilen
;
1106 ipcp_options
try; /* options to request next time */
1110 * Any Rejected CIs must be in exactly the same order that we sent.
1111 * Check packet length and CI length at each step.
1112 * If we find any deviations, then this packet is bad.
1114 #define REJCIADDR(opt, neg, old, val1, val2) \
1116 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
1123 cilong = htonl(l); \
1124 /* Check rejected value. */ \
1125 if (cilong != val1) \
1129 cilong = htonl(l); \
1130 /* Check rejected value. */ \
1131 if (cilong != val2) \
1137 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1139 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1144 GETSHORT(cishort, p); \
1145 /* Check rejected value. */ \
1146 if (cishort != val) \
1149 GETCHAR(cimaxslotindex, p); \
1150 if (cimaxslotindex != maxslot) \
1152 GETCHAR(ciflag, p); \
1153 if (ciflag != cflag) \
1159 #define REJCIDNS(opt, neg, dnsaddr) \
1161 ((cilen = p[1]) == CILEN_ADDR) && \
1168 cilong = htonl(l); \
1169 /* Check rejected value. */ \
1170 if (cilong != dnsaddr) \
1176 REJCIADDR((go
->old_addrs
? CI_ADDRS
: CI_ADDR
), neg_addr
,
1177 go
->old_addrs
, go
->ouraddr
, go
->hisaddr
);
1179 REJCIVJ(CI_COMPRESSTYPE
, neg_vj
, go
->vj_protocol
, go
->old_vj
,
1180 go
->maxslotindex
, go
->cflag
);
1182 REJCIDNS(CI_MS_DNS1
, req_dns1
, go
->dnsaddr
[0]);
1184 REJCIDNS(CI_MS_DNS2
, req_dns2
, go
->dnsaddr
[1]);
1187 * If there are any remaining CIs, then this packet is bad.
1192 * Now we can update state.
1194 if (f
->state
!= OPENED
)
1199 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1205 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1206 * Callback from fsm_rconfreq, Receive Configure Request
1208 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1209 * appropriately. If reject_if_disagree is non-zero, doesn't return
1210 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1213 ipcp_reqci(f
, inp
, len
, reject_if_disagree
)
1215 u_char
*inp
; /* Requested CIs */
1216 int *len
; /* Length of requested CIs */
1217 int reject_if_disagree
;
1219 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1220 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1221 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
1222 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1223 u_char
*cip
, *next
; /* Pointer to current and next CIs */
1224 u_short cilen
, citype
; /* Parsed len, type */
1225 u_short cishort
; /* Parsed short value */
1226 u_int32_t tl
, ciaddr1
, ciaddr2
;/* Parsed address values */
1227 int rc
= CONFACK
; /* Final packet return code */
1228 int orc
; /* Individual option return code */
1229 u_char
*p
; /* Pointer to next char to parse */
1230 u_char
*ucp
= inp
; /* Pointer to current output char */
1231 int l
= *len
; /* Length left */
1232 u_char maxslotindex
, cflag
;
1236 * Reset all his options.
1238 BZERO(ho
, sizeof(*ho
));
1241 * Process all his options.
1245 orc
= CONFACK
; /* Assume success */
1246 cip
= p
= next
; /* Remember begining of CI */
1247 if (l
< 2 || /* Not enough data for CI header or */
1248 p
[1] < 2 || /* CI length too small or */
1249 p
[1] > l
) { /* CI length too big? */
1250 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1251 orc
= CONFREJ
; /* Reject bad CI */
1252 cilen
= l
; /* Reject till end of packet */
1253 l
= 0; /* Don't loop again */
1256 GETCHAR(citype
, p
); /* Parse CI type */
1257 GETCHAR(cilen
, p
); /* Parse CI length */
1258 l
-= cilen
; /* Adjust remaining length */
1259 next
+= cilen
; /* Step to next CI */
1261 switch (citype
) { /* Check CI type */
1263 if (!ao
->neg_addr
||
1264 cilen
!= CILEN_ADDRS
) { /* Check CI length */
1265 orc
= CONFREJ
; /* Reject CI */
1270 * If he has no address, or if we both have his address but
1271 * disagree about it, then NAK it with our idea.
1272 * In particular, if we don't know his address, but he does,
1275 GETLONG(tl
, p
); /* Parse source address (his) */
1276 ciaddr1
= htonl(tl
);
1277 if (ciaddr1
!= wo
->hisaddr
1278 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
1280 if (!reject_if_disagree
) {
1281 DECPTR(sizeof(u_int32_t
), p
);
1282 tl
= ntohl(wo
->hisaddr
);
1285 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
1287 * If neither we nor he knows his address, reject the option.
1290 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
1295 * If he doesn't know our address, or if we both have our address
1296 * but disagree about it, then NAK it with our idea.
1298 GETLONG(tl
, p
); /* Parse desination address (ours) */
1299 ciaddr2
= htonl(tl
);
1300 if (ciaddr2
!= wo
->ouraddr
) {
1301 if (ciaddr2
== 0 || !wo
->accept_local
) {
1303 if (!reject_if_disagree
) {
1304 DECPTR(sizeof(u_int32_t
), p
);
1305 tl
= ntohl(wo
->ouraddr
);
1309 go
->ouraddr
= ciaddr2
; /* accept peer's idea */
1315 ho
->hisaddr
= ciaddr1
;
1316 ho
->ouraddr
= ciaddr2
;
1320 if (!ao
->neg_addr
||
1321 cilen
!= CILEN_ADDR
) { /* Check CI length */
1322 orc
= CONFREJ
; /* Reject CI */
1327 * If he has no address, or if we both have his address but
1328 * disagree about it, then NAK it with our idea.
1329 * In particular, if we don't know his address, but he does,
1332 GETLONG(tl
, p
); /* Parse source address (his) */
1333 ciaddr1
= htonl(tl
);
1334 if (ciaddr1
!= wo
->hisaddr
1335 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
1337 if (!reject_if_disagree
) {
1338 DECPTR(sizeof(u_int32_t
), p
);
1339 tl
= ntohl(wo
->hisaddr
);
1342 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
1344 * Don't ACK an address of 0.0.0.0 - reject it instead.
1347 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
1352 ho
->hisaddr
= ciaddr1
;
1357 /* Microsoft primary or secondary DNS request */
1358 d
= citype
== CI_MS_DNS2
;
1360 /* If we do not have a DNS address then we cannot send it */
1361 if (ao
->dnsaddr
[d
] == 0 ||
1362 cilen
!= CILEN_ADDR
) { /* Check CI length */
1363 orc
= CONFREJ
; /* Reject CI */
1367 if (htonl(tl
) != ao
->dnsaddr
[d
]) {
1368 DECPTR(sizeof(u_int32_t
), p
);
1369 tl
= ntohl(ao
->dnsaddr
[d
]);
1377 /* Microsoft primary or secondary WINS request */
1378 d
= citype
== CI_MS_WINS2
;
1380 /* If we do not have a DNS address then we cannot send it */
1381 if (ao
->winsaddr
[d
] == 0 ||
1382 cilen
!= CILEN_ADDR
) { /* Check CI length */
1383 orc
= CONFREJ
; /* Reject CI */
1387 if (htonl(tl
) != ao
->winsaddr
[d
]) {
1388 DECPTR(sizeof(u_int32_t
), p
);
1389 tl
= ntohl(ao
->winsaddr
[d
]);
1395 case CI_COMPRESSTYPE
:
1397 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
)) {
1401 GETSHORT(cishort
, p
);
1403 if (!(cishort
== IPCP_VJ_COMP
||
1404 (cishort
== IPCP_VJ_COMP_OLD
&& cilen
== CILEN_COMPRESS
))) {
1410 ho
->vj_protocol
= cishort
;
1411 if (cilen
== CILEN_VJ
) {
1412 GETCHAR(maxslotindex
, p
);
1413 if (maxslotindex
> ao
->maxslotindex
) {
1415 if (!reject_if_disagree
){
1417 PUTCHAR(ao
->maxslotindex
, p
);
1421 if (cflag
&& !ao
->cflag
) {
1423 if (!reject_if_disagree
){
1425 PUTCHAR(wo
->cflag
, p
);
1428 ho
->maxslotindex
= maxslotindex
;
1432 ho
->maxslotindex
= MAX_STATES
- 1;
1442 if (orc
== CONFACK
&& /* Good CI */
1443 rc
!= CONFACK
) /* but prior CI wasnt? */
1444 continue; /* Don't send this one */
1446 if (orc
== CONFNAK
) { /* Nak this CI? */
1447 if (reject_if_disagree
) /* Getting fed up with sending NAKs? */
1448 orc
= CONFREJ
; /* Get tough if so */
1450 if (rc
== CONFREJ
) /* Rejecting prior CI? */
1451 continue; /* Don't send this one */
1452 if (rc
== CONFACK
) { /* Ack'd all prior CIs? */
1453 rc
= CONFNAK
; /* Not anymore... */
1454 ucp
= inp
; /* Backup */
1459 if (orc
== CONFREJ
&& /* Reject this CI */
1460 rc
!= CONFREJ
) { /* but no prior ones? */
1462 ucp
= inp
; /* Backup */
1465 /* Need to move CI? */
1467 BCOPY(cip
, ucp
, cilen
); /* Move it */
1469 /* Update output pointer */
1474 * If we aren't rejecting this packet, and we want to negotiate
1475 * their address, and they didn't send their address, then we
1476 * send a NAK with a CI_ADDR option appended. We assume the
1477 * input buffer is long enough that we can append the extra
1480 if (rc
!= CONFREJ
&& !ho
->neg_addr
&&
1481 wo
->req_addr
&& !reject_if_disagree
) {
1482 if (rc
== CONFACK
) {
1484 ucp
= inp
; /* reset pointer */
1485 wo
->req_addr
= 0; /* don't ask again */
1487 PUTCHAR(CI_ADDR
, ucp
);
1488 PUTCHAR(CILEN_ADDR
, ucp
);
1489 tl
= ntohl(wo
->hisaddr
);
1493 *len
= ucp
- inp
; /* Compute output length */
1494 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc
)));
1495 return (rc
); /* Return final code */
1500 * ip_check_options - check that any IP-related options are OK,
1501 * and assign appropriate defaults.
1508 ipcp_options
*wo
= &ipcp_wantoptions
[0];
1511 * Default our local IP address based on our hostname.
1512 * If local IP address already given, don't bother.
1514 if (wo
->ouraddr
== 0 && !disable_defaultip
) {
1516 * Look up our hostname (possibly with domain name appended)
1517 * and take the first IP address as our local IP address.
1518 * If there isn't an IP address for our hostname, too bad.
1520 wo
->accept_local
= 1; /* don't insist on this default value */
1521 if ((hp
= gethostbyname(hostname
)) != NULL
) {
1522 local
= *(u_int32_t
*)hp
->h_addr
;
1523 if (local
!= 0 && !bad_ip_adrs(local
))
1524 wo
->ouraddr
= local
;
1527 ask_for_local
= wo
->ouraddr
!= 0 || !disable_defaultip
;
1532 * ip_demand_conf - configure the interface as though
1533 * IPCP were up, for use with dial-on-demand.
1539 ipcp_options
*wo
= &ipcp_wantoptions
[u
];
1541 if (wo
->hisaddr
== 0) {
1542 /* make up an arbitrary address for the peer */
1543 wo
->hisaddr
= htonl(0x0a707070 + ifunit
);
1544 wo
->accept_remote
= 1;
1546 if (wo
->ouraddr
== 0) {
1547 /* make up an arbitrary address for us */
1548 wo
->ouraddr
= htonl(0x0a404040 + ifunit
);
1549 wo
->accept_local
= 1;
1550 ask_for_local
= 0; /* don't tell the peer this address */
1552 if (!sifaddr(u
, wo
->ouraddr
, wo
->hisaddr
, GetMask(wo
->ouraddr
)))
1556 if (!sifnpmode(u
, PPP_IP
, NPMODE_QUEUE
))
1558 if (wo
->default_route
)
1559 if (sifdefaultroute(u
, wo
->ouraddr
, wo
->hisaddr
))
1560 default_route_set
[u
] = 1;
1562 if (sifproxyarp(u
, wo
->hisaddr
))
1563 proxy_arp_set
[u
] = 1;
1566 //if(wo->ouraddr != 0x4040400A && wo->hisaddr != 0x7070700A)
1568 // syslog(LOG_NOTICE, SYSLOG_PPPOE "Local IP address %d.%d.%d.%d", IP_FMT(wo->ouraddr));
1569 // syslog(LOG_NOTICE, SYSLOG_PPPOE "Remote IP address %d.%d.%d.%d", IP_FMT(wo->hisaddr));
1572 notice("local IP address %I", wo
->ouraddr
);
1573 notice("remote IP address %I", wo
->hisaddr
);
1581 * ipcp_up - IPCP has come UP.
1583 * Configure the IP network interface appropriately and bring it up.
1590 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1591 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1592 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1594 IPCPDEBUG(("ipcp: up"));
1597 * We must have a non-zero IP address for both ends of the link.
1600 ho
->hisaddr
= wo
->hisaddr
;
1602 if (go
->ouraddr
== 0) {
1603 LOGX_ERROR("Could not determine local IP address assigned by remote peer.");
1604 error("Could not determine local IP address");
1605 ipcp_close(f
->unit
, "Could not determine local IP address");
1608 if (ho
->hisaddr
== 0) {
1609 ho
->hisaddr
= htonl(0x0a404040 + ifunit
);
1610 warn("Could not determine remote IP address: defaulting to %I",
1613 #ifdef UNNUMBERIP_SUPPORT
1614 if(is_unnumber_ip
== 1){
1615 go
->ouraddr
= get_lan_ip(); // tallest 0129
1618 script_setenv("IPLOCAL", ip_ntoa(go
->ouraddr
), 0);
1619 script_setenv("IPREMOTE", ip_ntoa(ho
->hisaddr
), 1);
1621 if (usepeerdns
&& (go
->dnsaddr
[0] || go
->dnsaddr
[1])) {
1622 script_setenv("USEPEERDNS", "1", 0);
1624 script_setenv("DNS1", ip_ntoa(go
->dnsaddr
[0]), 0);
1626 script_setenv("DNS2", ip_ntoa(go
->dnsaddr
[1]), 0);
1627 create_resolv(go
->dnsaddr
[0], go
->dnsaddr
[1]);
1631 * Check that the peer is allowed to use the IP address it wants.
1633 if (!auth_ip_addr(f
->unit
, ho
->hisaddr
)) {
1634 LOGX_ERROR("Remote Peer is not authorized to use the address %d.%d.%d.%d.", IP_FMT(ho
->hisaddr
));
1635 error("Peer is not authorized to use remote address %I", ho
->hisaddr
);
1636 ipcp_close(f
->unit
, "Unauthorized remote IP address");
1640 /* set tcp compression */
1641 sifvjcomp(f
->unit
, ho
->neg_vj
, ho
->cflag
, ho
->maxslotindex
);
1644 * If we are doing dial-on-demand, the interface is already
1645 * configured, so we put out any saved-up packets, then set the
1646 * interface to pass IP packets.
1649 if (go
->ouraddr
!= wo
->ouraddr
|| ho
->hisaddr
!= wo
->hisaddr
) {
1650 ipcp_clear_addrs(f
->unit
, wo
->ouraddr
, wo
->hisaddr
);
1652 if (go
->ouraddr
!= wo
->ouraddr
) {
1653 warn("Local IP address changed to %I", go
->ouraddr
);
1654 script_setenv("OLDIPLOCAL", ip_ntoa(wo
->ouraddr
), 0);
1655 wo
->ouraddr
= go
->ouraddr
;
1658 script_unsetenv("OLDIPLOCAL");
1660 if (ho
->hisaddr
!= wo
->hisaddr
) {
1661 warn("Remote IP address changed to %I", ho
->hisaddr
);
1662 script_setenv("OLDIPREMOTE", ip_ntoa(wo
->hisaddr
), 0);
1663 wo
->hisaddr
= ho
->hisaddr
;
1666 script_unsetenv("OLDIPREMOTE");
1668 /* Set the interface to the new addresses */
1669 mask
= GetMask(go
->ouraddr
);
1670 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1672 warn("Interface configuration failed");
1673 ipcp_close(f
->unit
, "Interface configuration failed");
1677 /* assign a default route through the interface if required */
1678 if (ipcp_wantoptions
[f
->unit
].default_route
)
1679 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1680 default_route_set
[f
->unit
] = 1;
1682 /* Make a proxy ARP entry if requested. */
1683 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1684 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1685 proxy_arp_set
[f
->unit
] = 1;
1687 demand_rexmit(PPP_IP
);
1688 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1693 * Set IP addresses and (if specified) netmask.
1695 mask
= GetMask(go
->ouraddr
);
1697 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1698 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1700 warn("Interface configuration failed");
1701 ipcp_close(f
->unit
, "Interface configuration failed");
1706 /* bring the interface up for IP */
1707 if (!sifup(f
->unit
)) {
1709 warn("Interface failed to come up");
1710 ipcp_close(f
->unit
, "Interface configuration failed");
1714 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1715 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1717 warn("Interface configuration failed");
1718 ipcp_close(f
->unit
, "Interface configuration failed");
1722 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1724 /* assign a default route through the interface if required */
1725 if (ipcp_wantoptions
[f
->unit
].default_route
)
1726 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1727 default_route_set
[f
->unit
] = 1;
1729 /* Make a proxy ARP entry if requested. */
1730 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1731 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1732 proxy_arp_set
[f
->unit
] = 1;
1734 ipcp_wantoptions
[0].ouraddr
= go
->ouraddr
;
1737 LOGX_NOTICE("Connected.");
1738 LOGX_NOTICE("IP Address: %d.%d.%d.%d", IP_FMT(go
->ouraddr
));
1739 LOGX_NOTICE("DNS Address: %d.%d.%d.%d, %d.%d.%d.%d", IP_FMT(go
->dnsaddr
[0]), IP_FMT(go
->dnsaddr
[1]));
1741 np_up(f
->unit
, PPP_IP
);
1748 * Execute the ip-up script, like this:
1749 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1751 if (ipcp_script_state
== s_down
&& ipcp_script_pid
== 0) {
1752 ipcp_script_state
= s_up
;
1753 ipcp_script(_PATH_IPUP
);
1757 //=============================================================================
1761 kill_ppp(int ppp_num)
1763 #ifdef MPPPOE_SUPPORT
1768 sprintf(buf,"%.500s %d",ppp_disconnect_func,ppp_num);
1769 info("tallest:=====(Executing external command - %s)=====\n",buf);
1775 //=============================================================================
1778 * ipcp_down - IPCP has gone DOWN.
1780 * Take the IP network interface down, clear its addresses
1781 * and delete routes through it.
1787 IPCPDEBUG(("ipcp: down"));
1788 update_link_stats(f
->unit
);
1793 np_down(f
->unit
, PPP_IP
);
1795 sifvjcomp(f
->unit
, 0, 0, 0);
1798 * If we are doing dial-on-demand, set the interface
1799 * to queue up outgoing packets (for now).
1802 sifnpmode(f
->unit
, PPP_IP
, NPMODE_QUEUE
);
1804 sifnpmode(f
->unit
, PPP_IP
, NPMODE_DROP
);
1806 ipcp_clear_addrs(f
->unit
, ipcp_gotoptions
[f
->unit
].ouraddr
,
1807 ipcp_hisoptions
[f
->unit
].hisaddr
);
1810 /* Execute the ip-down script */
1811 if (ipcp_script_state
== s_up
&& ipcp_script_pid
== 0) {
1812 ipcp_script_state
= s_down
;
1813 ipcp_script(_PATH_IPDOWN
);
1814 #ifdef MPPPOE_SUPPORT
1815 kill_ppp(atoi(ipparam
)); // by tallest 0407
1822 * ipcp_clear_addrs() - clear the interface addresses, routes,
1823 * proxy arp entries, etc.
1826 ipcp_clear_addrs(unit
, ouraddr
, hisaddr
)
1828 u_int32_t ouraddr
; /* local address */
1829 u_int32_t hisaddr
; /* remote address */
1831 if (proxy_arp_set
[unit
]) {
1832 cifproxyarp(unit
, hisaddr
);
1833 proxy_arp_set
[unit
] = 0;
1835 if (default_route_set
[unit
]) {
1836 cifdefaultroute(unit
, ouraddr
, hisaddr
);
1837 default_route_set
[unit
] = 0;
1839 cifaddr(unit
, ouraddr
, hisaddr
);
1844 * ipcp_finished - possibly shut down the lower layers.
1850 np_finished(f
->unit
, PPP_IP
);
1855 * ipcp_script_done - called when the ip-up or ip-down script
1859 ipcp_script_done(arg
)
1862 ipcp_script_pid
= 0;
1863 switch (ipcp_script_state
) {
1865 if (ipcp_fsm
[0].state
!= OPENED
) {
1866 ipcp_script_state
= s_down
;
1867 ipcp_script(_PATH_IPDOWN
);
1871 if (ipcp_fsm
[0].state
== OPENED
) {
1872 ipcp_script_state
= s_up
;
1873 ipcp_script(_PATH_IPUP
);
1881 * ipcp_script - Execute a script with arguments
1882 * interface-name tty-name speed local-IP remote-IP.
1888 char strspeed
[32], strlocal
[32], strremote
[32];
1891 slprintf(strspeed
, sizeof(strspeed
), "%d", baud_rate
);
1892 slprintf(strlocal
, sizeof(strlocal
), "%I", ipcp_gotoptions
[0].ouraddr
);
1893 slprintf(strremote
, sizeof(strremote
), "%I", ipcp_hisoptions
[0].hisaddr
);
1900 argv
[5] = strremote
;
1903 ipcp_script_pid
= run_program(script
, argv
, 0, ipcp_script_done
, NULL
);
1906 #ifdef UNNUMBERIP_SUPPORT
1908 * get_lan_ip - get LAN IP for unnumber ip use.
1910 #define s_addr(s) ( (((struct sockaddr_in *)(s))->sin_addr).s_addr )
1912 get_lan_ip( void ) // tallest 0129
1917 if ((s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0)
1919 strncpy(ifr
.ifr_name
, "br0", IFNAMSIZ
);
1922 while (ioctl(s
, SIOCGIFADDR
, &ifr
) && timeout
--)
1924 info("Wait br0 inteface to init (%d) ...\n",timeout
);
1926 info("tallest:Using Unnumber IP ==> ifr.ifr_addr = %x <==\n", s_addr(&(ifr
.ifr_addr
)));
1927 return s_addr( &(ifr
.ifr_addr
) );
1932 * create_resolv - create the replacement resolv.conf file
1935 create_resolv(peerdns1
, peerdns2
)
1936 u_int32_t peerdns1
, peerdns2
;
1940 f
= fopen(_PATH_RESOLV
, "w");
1942 error("Failed to create %s: %m", _PATH_RESOLV
);
1947 fprintf(f
, "nameserver %s\n", ip_ntoa(peerdns1
));
1950 fprintf(f
, "nameserver %s\n", ip_ntoa(peerdns2
));
1953 error("Write failed to %s: %m", _PATH_RESOLV
);
1959 * ipcp_printpkt - print the contents of an IPCP packet.
1961 static char *ipcp_codenames
[] = {
1962 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1963 "TermReq", "TermAck", "CodeRej"
1967 ipcp_printpkt(p
, plen
, printer
, arg
)
1970 void (*printer
) __P((void *, char *, ...));
1973 int code
, id
, len
, olen
;
1974 u_char
*pstart
, *optend
;
1978 if (plen
< HEADERLEN
)
1984 if (len
< HEADERLEN
|| len
> plen
)
1987 if (code
>= 1 && code
<= sizeof(ipcp_codenames
) / sizeof(char *))
1988 printer(arg
, " %s", ipcp_codenames
[code
-1]);
1990 printer(arg
, " code=0x%x", code
);
1991 printer(arg
, " id=0x%x", id
);
1998 /* print option list */
2003 if (olen
< 2 || olen
> len
) {
2011 if (olen
== CILEN_ADDRS
) {
2014 printer(arg
, "addrs %I", htonl(cilong
));
2016 printer(arg
, " %I", htonl(cilong
));
2019 case CI_COMPRESSTYPE
:
2020 if (olen
>= CILEN_COMPRESS
) {
2022 GETSHORT(cishort
, p
);
2023 printer(arg
, "compress ");
2028 case IPCP_VJ_COMP_OLD
:
2029 printer(arg
, "old-VJ");
2032 printer(arg
, "0x%x", cishort
);
2037 if (olen
== CILEN_ADDR
) {
2040 printer(arg
, "addr %I", htonl(cilong
));
2047 printer(arg
, "ms-dns%d %I", code
- CI_MS_DNS1
+ 1,
2054 printer(arg
, "ms-wins %I", htonl(cilong
));
2057 while (p
< optend
) {
2059 printer(arg
, " %.2x", code
);
2067 if (len
> 0 && *p
>= ' ' && *p
< 0x7f) {
2069 print_string((char *)p
, len
, printer
, arg
);
2076 /* print the rest of the bytes in the packet */
2077 for (; len
> 0; --len
) {
2079 printer(arg
, " %.2x", code
);
2086 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2087 * We don't bring the link up for IP fragments or for TCP FIN packets
2090 #define IP_HDRLEN 20 /* bytes */
2091 #define IP_OFFMASK 0x1fff
2092 // #define IPPROTO_TCP 6
2093 #define TCP_HDRLEN 20
2097 * We use these macros because the IP header may be at an odd address,
2098 * and some compilers might use word loads to get th_off or ip_hl.
2101 #define net_short(x) (((x)[0] << 8) + (x)[1])
2102 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2103 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2104 #define get_ipproto(x) (((unsigned char *)(x))[9])
2105 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2106 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2109 ip_active_pkt(pkt
, len
)
2118 if (len
< IP_HDRLEN
)
2120 if ((get_ipoff(pkt
) & IP_OFFMASK
) != 0)
2122 if (get_ipproto(pkt
) != IPPROTO_TCP
)
2124 hlen
= get_iphl(pkt
) * 4;
2125 if (len
< hlen
+ TCP_HDRLEN
)
2128 if ((get_tcpflags(tcp
) & TH_FIN
) != 0 && len
== hlen
+ get_tcpoff(tcp
) * 4)