CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / ppp / pppd / ipcp.c
blob88c77b38912aae0529ba5b611148521dc4963609
1 /*
2 * ipcp.c - PPP IP Control Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
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 $"
23 * TODO:
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <netdb.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #ifdef UNNUMBERIP_SUPPORT
34 #include <sys/ioctl.h>
35 #include <linux/if.h>
36 #endif
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
42 #include <pppd.h>
43 #include "fsm.h"
44 #include "ipcp.h"
45 #include "pathnames.h"
47 static const char rcsid[] = RCSID;
49 /* global vars */
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;
69 /* local vars */
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 },
198 { NULL }
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 = {
219 PPP_IPCP,
220 ipcp_init,
221 ipcp_input,
222 ipcp_protrej,
223 ipcp_lowerup,
224 ipcp_lowerdown,
225 ipcp_open,
226 ipcp_close,
227 ipcp_printpkt,
228 NULL,
230 "IPCP",
231 "IP",
232 ipcp_option_list,
233 ip_check_options,
234 ip_demand_conf,
235 ip_active_pkt
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
247 #endif
250 * Lengths of configuration options.
252 #define CILEN_VOID 2
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 {
267 s_down,
268 s_up,
269 } ipcp_script_state;
270 static pid_t ipcp_script_pid;
273 * Make a string representation of a network IP address.
275 char *
276 ip_ntoa(ipaddr)
277 u_int32_t ipaddr;
279 static char b[64];
281 slprintf(b, sizeof(b), "%I", ipaddr);
282 return b;
286 * Option parsing.
290 * setvjslots - set maximum number of connection slots for VJ compression
292 static int
293 setvjslots(argv)
294 char **argv;
296 int value = 0;
298 if (!int_option(*argv, &value))
299 return 0;
300 if (value < 2 || value > 16) {
301 option_error("vj-max-slots value must be between 2 and 16");
302 return 0;
304 ipcp_wantoptions [0].maxslotindex =
305 ipcp_allowoptions[0].maxslotindex = value - 1;
306 slprintf(vj_value, sizeof(vj_value), "%d", value);
307 return 1;
311 * setdnsaddr - set the dns address(es)
313 static int
314 setdnsaddr(argv)
315 char **argv;
317 u_int32_t dns;
318 struct hostent *hp;
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",
324 *argv);
325 return 0;
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;
335 else
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;
341 return (1);
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.
349 static int
350 setwinsaddr(argv)
351 char **argv;
353 u_int32_t wins;
354 struct hostent *hp;
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",
360 *argv);
361 return 0;
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;
371 else
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;
377 return (1);
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.
385 static int
386 setipaddr(arg, argv, doit)
387 char *arg;
388 char **argv;
389 int doit;
391 struct hostent *hp;
392 char *colon;
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)
401 return 0;
402 if (!doit)
403 return 1;
406 * If colon first character, then no local addr.
408 if (colon != arg && option_priority >= prio_local) {
409 *colon = '\0';
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);
415 return 0;
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));
422 return 0;
424 if (local != 0)
425 wo->ouraddr = local;
426 *colon = ':';
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);
438 return 0;
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));
447 return 0;
449 if (remote != 0)
450 wo->hisaddr = remote;
451 prio_remote = option_priority;
454 return 1;
457 static void
458 printipaddr(opt, printer, arg)
459 option_t *opt;
460 void (*printer) __P((void *, char *, ...));
461 void *arg;
463 ipcp_options *wo = &ipcp_wantoptions[0];
465 if (wo->ouraddr != 0)
466 printer(arg, "%I", wo->ouraddr);
467 printer(arg, ":");
468 if (wo->hisaddr != 0)
469 printer(arg, "%I", wo->hisaddr);
473 * setnetmask - set the netmask to be used on the interface.
475 static int
476 setnetmask(argv)
477 char **argv;
479 u_int32_t mask;
480 int n;
481 char *p;
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.
487 p = *argv;
488 n = parse_dotted_ip(p, &mask);
490 mask = htonl(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);
495 return 0;
498 netmask = mask;
499 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
501 return (1);
505 parse_dotted_ip(p, vp)
506 char *p;
507 u_int32_t *vp;
509 int n;
510 u_int32_t v, b;
511 char *endp, *p0 = p;
513 v = 0;
514 for (n = 3;; --n) {
515 b = strtoul(p, &endp, 0);
516 if (endp == p)
517 return 0;
518 if (b > 255) {
519 if (n < 3)
520 return 0;
521 /* accept e.g. 0xffffff00 */
522 *vp = b;
523 return endp - p0;
525 v |= b << (n * 8);
526 p = endp;
527 if (n == 0)
528 break;
529 if (*p != '.')
530 return 0;
531 ++p;
533 *vp = v;
534 return p - p0;
539 * ipcp_init - Initialize IPCP.
541 static void
542 ipcp_init(unit)
543 int unit;
545 fsm *f = &ipcp_fsm[unit];
546 ipcp_options *wo = &ipcp_wantoptions[unit];
547 ipcp_options *ao = &ipcp_allowoptions[unit];
549 f->unit = 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));
557 wo->neg_addr = 1;
558 wo->neg_vj = 1;
559 wo->vj_protocol = IPCP_VJ_COMP;
560 wo->maxslotindex = MAX_STATES - 1; /* really max index */
561 wo->cflag = 1;
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 */
566 /* things) gmc */
568 ao->neg_addr = 1;
569 ao->neg_vj = 1;
570 ao->maxslotindex = MAX_STATES - 1;
571 ao->cflag = 1;
573 ao->proxy_arp = 1;
574 ao->default_route = 1;
579 * ipcp_open - IPCP is allowed to come up.
581 static void
582 ipcp_open(unit)
583 int unit;
585 fsm_open(&ipcp_fsm[unit]);
590 * ipcp_close - Take IPCP down.
592 static void
593 ipcp_close(unit, reason)
594 int unit;
595 char *reason;
597 fsm_close(&ipcp_fsm[unit], reason);
602 * ipcp_lowerup - The lower layer is up.
604 static void
605 ipcp_lowerup(unit)
606 int unit;
608 fsm_lowerup(&ipcp_fsm[unit]);
613 * ipcp_lowerdown - The lower layer is down.
615 static void
616 ipcp_lowerdown(unit)
617 int unit;
619 fsm_lowerdown(&ipcp_fsm[unit]);
624 * ipcp_input - Input IPCP packet.
626 static void
627 ipcp_input(unit, p, len)
628 int unit;
629 u_char *p;
630 int 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.
641 static void
642 ipcp_protrej(unit)
643 int unit;
645 fsm_lowerdown(&ipcp_fsm[unit]);
650 * ipcp_resetci - Reset our CI.
651 * Called by fsm_sconfreq, Send Configure Request.
653 static void
654 ipcp_resetci(f)
655 fsm *f;
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;
667 *go = *wo;
668 if (!ask_for_local)
669 go->ouraddr = 0;
670 if (ip_choose_hook)
671 ip_choose_hook(&wo->hisaddr);
676 * ipcp_cilen - Return length of our CI.
677 * Called by fsm_sconfreq, Send Configure Request.
679 static int
680 ipcp_cilen(f)
681 fsm *f;
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 */
697 go->neg_addr = 1;
698 go->old_addrs = 1;
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) {
704 go->neg_vj = 1;
705 go->old_vj = 1;
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.
721 static void
722 ipcp_addci(f, ucp, lenp)
723 fsm *f;
724 u_char *ucp;
725 int *lenp;
727 ipcp_options *go = &ipcp_gotoptions[f->unit];
728 int len = *lenp;
730 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
731 if (neg) { \
732 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
733 if (len >= vjlen) { \
734 PUTCHAR(opt, ucp); \
735 PUTCHAR(vjlen, ucp); \
736 PUTSHORT(val, ucp); \
737 if (!old) { \
738 PUTCHAR(maxslotindex, ucp); \
739 PUTCHAR(cflag, ucp); \
741 len -= vjlen; \
742 } else \
743 neg = 0; \
746 #define ADDCIADDR(opt, neg, old, val1, val2) \
747 if (neg) { \
748 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
749 if (len >= addrlen) { \
750 u_int32_t l; \
751 PUTCHAR(opt, ucp); \
752 PUTCHAR(addrlen, ucp); \
753 l = ntohl(val1); \
754 PUTLONG(l, ucp); \
755 if (old) { \
756 l = ntohl(val2); \
757 PUTLONG(l, ucp); \
759 len -= addrlen; \
760 } else \
761 neg = 0; \
764 #define ADDCIDNS(opt, neg, addr) \
765 if (neg) { \
766 if (len >= CILEN_ADDR) { \
767 u_int32_t l; \
768 PUTCHAR(opt, ucp); \
769 PUTCHAR(CILEN_ADDR, ucp); \
770 l = ntohl(addr); \
771 PUTLONG(l, ucp); \
772 len -= CILEN_ADDR; \
773 } else \
774 neg = 0; \
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]);
787 *lenp -= len;
792 * ipcp_ackci - Ack our CIs.
793 * Called by fsm_rconfack, Receive Configure ACK.
795 * Returns:
796 * 0 - Ack was bad.
797 * 1 - Ack was good.
799 static int
800 ipcp_ackci(f, p, len)
801 fsm *f;
802 u_char *p;
803 int len;
805 ipcp_options *go = &ipcp_gotoptions[f->unit];
806 u_short cilen, citype, cishort;
807 u_int32_t cilong;
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) \
817 if (neg) { \
818 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
819 if ((len -= vjlen) < 0) \
820 goto bad; \
821 GETCHAR(citype, p); \
822 GETCHAR(cilen, p); \
823 if (cilen != vjlen || \
824 citype != opt) \
825 goto bad; \
826 GETSHORT(cishort, p); \
827 if (cishort != val) \
828 goto bad; \
829 if (!old) { \
830 GETCHAR(cimaxslotindex, p); \
831 if (cimaxslotindex != maxslotindex) \
832 goto bad; \
833 GETCHAR(cicflag, p); \
834 if (cicflag != cflag) \
835 goto bad; \
839 #define ACKCIADDR(opt, neg, old, val1, val2) \
840 if (neg) { \
841 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
842 u_int32_t l; \
843 if ((len -= addrlen) < 0) \
844 goto bad; \
845 GETCHAR(citype, p); \
846 GETCHAR(cilen, p); \
847 if (cilen != addrlen || \
848 citype != opt) \
849 goto bad; \
850 GETLONG(l, p); \
851 cilong = htonl(l); \
852 if (val1 != cilong) \
853 goto bad; \
854 if (old) { \
855 GETLONG(l, p); \
856 cilong = htonl(l); \
857 if (val2 != cilong) \
858 goto bad; \
862 #define ACKCIDNS(opt, neg, addr) \
863 if (neg) { \
864 u_int32_t l; \
865 if ((len -= CILEN_ADDR) < 0) \
866 goto bad; \
867 GETCHAR(citype, p); \
868 GETCHAR(cilen, p); \
869 if (cilen != CILEN_ADDR || citype != opt) \
870 goto bad; \
871 GETLONG(l, p); \
872 cilong = htonl(l); \
873 if (addr != cilong) \
874 goto bad; \
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.
890 if (len != 0)
891 goto bad;
892 return (1);
894 bad:
895 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
896 return (0);
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.
905 * Returns:
906 * 0 - Nak was bad.
907 * 1 - Nak was good.
909 static int
910 ipcp_nakci(f, p, len)
911 fsm *f;
912 u_char *p;
913 int len;
915 ipcp_options *go = &ipcp_gotoptions[f->unit];
916 u_char cimaxslotindex, cicflag;
917 u_char citype, cilen, *next;
918 u_short cishort;
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));
924 try = *go;
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) \
932 if (go->neg && \
933 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
934 p[1] == cilen && \
935 p[0] == opt) { \
936 len -= cilen; \
937 INCPTR(2, p); \
938 GETLONG(l, p); \
939 ciaddr1 = htonl(l); \
940 if (old) { \
941 GETLONG(l, p); \
942 ciaddr2 = htonl(l); \
943 no.old_addrs = 1; \
944 } else \
945 ciaddr2 = 0; \
946 no.neg = 1; \
947 code \
950 #define NAKCIVJ(opt, neg, code) \
951 if (go->neg && \
952 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
953 len >= cilen && \
954 p[0] == opt) { \
955 len -= cilen; \
956 INCPTR(2, p); \
957 GETSHORT(cishort, p); \
958 no.neg = 1; \
959 code \
962 #define NAKCIDNS(opt, neg, code) \
963 if (go->neg && \
964 ((cilen = p[1]) == CILEN_ADDR) && \
965 len >= cilen && \
966 p[0] == opt) { \
967 len -= cilen; \
968 INCPTR(2, p); \
969 GETLONG(l, p); \
970 cidnsaddr = htonl(l); \
971 no.neg = 1; \
972 code \
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
992 * the peer wants.
994 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
995 if (cilen == CILEN_VJ) {
996 GETCHAR(cimaxslotindex, p);
997 GETCHAR(cicflag, p);
998 if (cishort == IPCP_VJ_COMP) {
999 try.old_vj = 0;
1000 if (cimaxslotindex < go->maxslotindex)
1001 try.maxslotindex = cimaxslotindex;
1002 if (!cicflag)
1003 try.cflag = 0;
1004 } else {
1005 try.neg_vj = 0;
1007 } else {
1008 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1009 try.old_vj = 1;
1010 try.vj_protocol = cishort;
1011 } else {
1012 try.neg_vj = 0;
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) {
1032 GETCHAR(citype, p);
1033 GETCHAR(cilen, p);
1034 if( (len -= cilen) < 0 )
1035 goto bad;
1036 next = p + cilen - 2;
1038 switch (citype) {
1039 case CI_COMPRESSTYPE:
1040 if (go->neg_vj || no.neg_vj ||
1041 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1042 goto bad;
1043 no.neg_vj = 1;
1044 break;
1045 case CI_ADDRS:
1046 if ((go->neg_addr && go->old_addrs) || no.old_addrs
1047 || cilen != CILEN_ADDRS)
1048 goto bad;
1049 try.neg_addr = 1;
1050 try.old_addrs = 1;
1051 GETLONG(l, p);
1052 ciaddr1 = htonl(l);
1053 if (ciaddr1 && go->accept_local)
1054 try.ouraddr = ciaddr1;
1055 GETLONG(l, p);
1056 ciaddr2 = htonl(l);
1057 if (ciaddr2 && go->accept_remote)
1058 try.hisaddr = ciaddr2;
1059 no.old_addrs = 1;
1060 break;
1061 case CI_ADDR:
1062 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1063 goto bad;
1064 try.old_addrs = 0;
1065 GETLONG(l, p);
1066 ciaddr1 = htonl(l);
1067 if (ciaddr1 && go->accept_local)
1068 try.ouraddr = ciaddr1;
1069 if (try.ouraddr != 0)
1070 try.neg_addr = 1;
1071 no.neg_addr = 1;
1072 break;
1074 p = next;
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)
1082 *go = try;
1084 return 1;
1086 bad:
1087 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1088 return 0;
1093 * ipcp_rejci - Reject some of our CIs.
1094 * Callback from fsm_rconfnakrej.
1096 static int
1097 ipcp_rejci(f, p, len)
1098 fsm *f;
1099 u_char *p;
1100 int len;
1102 ipcp_options *go = &ipcp_gotoptions[f->unit];
1103 u_char cimaxslotindex, ciflag, cilen;
1104 u_short cishort;
1105 u_int32_t cilong;
1106 ipcp_options try; /* options to request next time */
1108 try = *go;
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) \
1115 if (go->neg && \
1116 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
1117 p[1] == cilen && \
1118 p[0] == opt) { \
1119 u_int32_t l; \
1120 len -= cilen; \
1121 INCPTR(2, p); \
1122 GETLONG(l, p); \
1123 cilong = htonl(l); \
1124 /* Check rejected value. */ \
1125 if (cilong != val1) \
1126 goto bad; \
1127 if (old) { \
1128 GETLONG(l, p); \
1129 cilong = htonl(l); \
1130 /* Check rejected value. */ \
1131 if (cilong != val2) \
1132 goto bad; \
1134 try.neg = 0; \
1137 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1138 if (go->neg && \
1139 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1140 len >= p[1] && \
1141 p[0] == opt) { \
1142 len -= p[1]; \
1143 INCPTR(2, p); \
1144 GETSHORT(cishort, p); \
1145 /* Check rejected value. */ \
1146 if (cishort != val) \
1147 goto bad; \
1148 if (!old) { \
1149 GETCHAR(cimaxslotindex, p); \
1150 if (cimaxslotindex != maxslot) \
1151 goto bad; \
1152 GETCHAR(ciflag, p); \
1153 if (ciflag != cflag) \
1154 goto bad; \
1156 try.neg = 0; \
1159 #define REJCIDNS(opt, neg, dnsaddr) \
1160 if (go->neg && \
1161 ((cilen = p[1]) == CILEN_ADDR) && \
1162 len >= cilen && \
1163 p[0] == opt) { \
1164 u_int32_t l; \
1165 len -= cilen; \
1166 INCPTR(2, p); \
1167 GETLONG(l, p); \
1168 cilong = htonl(l); \
1169 /* Check rejected value. */ \
1170 if (cilong != dnsaddr) \
1171 goto bad; \
1172 try.neg = 0; \
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.
1189 if (len != 0)
1190 goto bad;
1192 * Now we can update state.
1194 if (f->state != OPENED)
1195 *go = try;
1196 return 1;
1198 bad:
1199 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1200 return 0;
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.
1212 static int
1213 ipcp_reqci(f, inp, len, reject_if_disagree)
1214 fsm *f;
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;
1233 int d;
1236 * Reset all his options.
1238 BZERO(ho, sizeof(*ho));
1241 * Process all his options.
1243 next = inp;
1244 while (l) {
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 */
1254 goto endswitch;
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 */
1262 case CI_ADDRS:
1263 if (!ao->neg_addr ||
1264 cilen != CILEN_ADDRS) { /* Check CI length */
1265 orc = CONFREJ; /* Reject CI */
1266 break;
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,
1273 * then accept it.
1275 GETLONG(tl, p); /* Parse source address (his) */
1276 ciaddr1 = htonl(tl);
1277 if (ciaddr1 != wo->hisaddr
1278 && (ciaddr1 == 0 || !wo->accept_remote)) {
1279 orc = CONFNAK;
1280 if (!reject_if_disagree) {
1281 DECPTR(sizeof(u_int32_t), p);
1282 tl = ntohl(wo->hisaddr);
1283 PUTLONG(tl, p);
1285 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1287 * If neither we nor he knows his address, reject the option.
1289 orc = CONFREJ;
1290 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1291 break;
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) {
1302 orc = CONFNAK;
1303 if (!reject_if_disagree) {
1304 DECPTR(sizeof(u_int32_t), p);
1305 tl = ntohl(wo->ouraddr);
1306 PUTLONG(tl, p);
1308 } else {
1309 go->ouraddr = ciaddr2; /* accept peer's idea */
1313 ho->neg_addr = 1;
1314 ho->old_addrs = 1;
1315 ho->hisaddr = ciaddr1;
1316 ho->ouraddr = ciaddr2;
1317 break;
1319 case CI_ADDR:
1320 if (!ao->neg_addr ||
1321 cilen != CILEN_ADDR) { /* Check CI length */
1322 orc = CONFREJ; /* Reject CI */
1323 break;
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,
1330 * then accept it.
1332 GETLONG(tl, p); /* Parse source address (his) */
1333 ciaddr1 = htonl(tl);
1334 if (ciaddr1 != wo->hisaddr
1335 && (ciaddr1 == 0 || !wo->accept_remote)) {
1336 orc = CONFNAK;
1337 if (!reject_if_disagree) {
1338 DECPTR(sizeof(u_int32_t), p);
1339 tl = ntohl(wo->hisaddr);
1340 PUTLONG(tl, p);
1342 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1344 * Don't ACK an address of 0.0.0.0 - reject it instead.
1346 orc = CONFREJ;
1347 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1348 break;
1351 ho->neg_addr = 1;
1352 ho->hisaddr = ciaddr1;
1353 break;
1355 case CI_MS_DNS1:
1356 case CI_MS_DNS2:
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 */
1364 break;
1366 GETLONG(tl, p);
1367 if (htonl(tl) != ao->dnsaddr[d]) {
1368 DECPTR(sizeof(u_int32_t), p);
1369 tl = ntohl(ao->dnsaddr[d]);
1370 PUTLONG(tl, p);
1371 orc = CONFNAK;
1373 break;
1375 case CI_MS_WINS1:
1376 case CI_MS_WINS2:
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 */
1384 break;
1386 GETLONG(tl, p);
1387 if (htonl(tl) != ao->winsaddr[d]) {
1388 DECPTR(sizeof(u_int32_t), p);
1389 tl = ntohl(ao->winsaddr[d]);
1390 PUTLONG(tl, p);
1391 orc = CONFNAK;
1393 break;
1395 case CI_COMPRESSTYPE:
1396 if (!ao->neg_vj ||
1397 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1398 orc = CONFREJ;
1399 break;
1401 GETSHORT(cishort, p);
1403 if (!(cishort == IPCP_VJ_COMP ||
1404 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1405 orc = CONFREJ;
1406 break;
1409 ho->neg_vj = 1;
1410 ho->vj_protocol = cishort;
1411 if (cilen == CILEN_VJ) {
1412 GETCHAR(maxslotindex, p);
1413 if (maxslotindex > ao->maxslotindex) {
1414 orc = CONFNAK;
1415 if (!reject_if_disagree){
1416 DECPTR(1, p);
1417 PUTCHAR(ao->maxslotindex, p);
1420 GETCHAR(cflag, p);
1421 if (cflag && !ao->cflag) {
1422 orc = CONFNAK;
1423 if (!reject_if_disagree){
1424 DECPTR(1, p);
1425 PUTCHAR(wo->cflag, p);
1428 ho->maxslotindex = maxslotindex;
1429 ho->cflag = cflag;
1430 } else {
1431 ho->old_vj = 1;
1432 ho->maxslotindex = MAX_STATES - 1;
1433 ho->cflag = 1;
1435 break;
1437 default:
1438 orc = CONFREJ;
1439 break;
1441 endswitch:
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 */
1449 else {
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? */
1461 rc = CONFREJ;
1462 ucp = inp; /* Backup */
1465 /* Need to move CI? */
1466 if (ucp != cip)
1467 BCOPY(cip, ucp, cilen); /* Move it */
1469 /* Update output pointer */
1470 INCPTR(cilen, ucp);
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
1478 * option safely.
1480 if (rc != CONFREJ && !ho->neg_addr &&
1481 wo->req_addr && !reject_if_disagree) {
1482 if (rc == CONFACK) {
1483 rc = CONFNAK;
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);
1490 PUTLONG(tl, ucp);
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.
1503 static void
1504 ip_check_options()
1506 struct hostent *hp;
1507 u_int32_t local;
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.
1535 static int
1536 ip_demand_conf(u)
1537 int u;
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)))
1553 return 0;
1554 if (!sifup(u))
1555 return 0;
1556 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1557 return 0;
1558 if (wo->default_route)
1559 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1560 default_route_set[u] = 1;
1561 if (wo->proxy_arp)
1562 if (sifproxyarp(u, wo->hisaddr))
1563 proxy_arp_set[u] = 1;
1565 #if 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));
1571 #else
1572 notice("local IP address %I", wo->ouraddr);
1573 notice("remote IP address %I", wo->hisaddr);
1574 #endif
1576 return 1;
1581 * ipcp_up - IPCP has come UP.
1583 * Configure the IP network interface appropriately and bring it up.
1585 static void
1586 ipcp_up(f)
1587 fsm *f;
1589 u_int32_t mask;
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.
1599 if (!ho->neg_addr)
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");
1606 return;
1608 if (ho->hisaddr == 0) {
1609 ho->hisaddr = htonl(0x0a404040 + ifunit);
1610 warn("Could not determine remote IP address: defaulting to %I",
1611 ho->hisaddr);
1613 #ifdef UNNUMBERIP_SUPPORT
1614 if(is_unnumber_ip == 1){
1615 go->ouraddr = get_lan_ip(); // tallest 0129
1617 #endif
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);
1623 if (go->dnsaddr[0])
1624 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1625 if (go->dnsaddr[1])
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");
1637 return;
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.
1648 if (demand) {
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;
1657 else
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;
1665 else
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)) {
1671 if (debug)
1672 warn("Interface configuration failed");
1673 ipcp_close(f->unit, "Interface configuration failed");
1674 return;
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);
1691 else {
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)) {
1699 if (debug)
1700 warn("Interface configuration failed");
1701 ipcp_close(f->unit, "Interface configuration failed");
1702 return;
1704 #endif
1706 /* bring the interface up for IP */
1707 if (!sifup(f->unit)) {
1708 if (debug)
1709 warn("Interface failed to come up");
1710 ipcp_close(f->unit, "Interface configuration failed");
1711 return;
1714 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1715 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1716 if (debug)
1717 warn("Interface configuration failed");
1718 ipcp_close(f->unit, "Interface configuration failed");
1719 return;
1721 #endif
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);
1742 ipcp_is_up = 1;
1744 if (ip_up_hook)
1745 ip_up_hook();
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 //=============================================================================
1758 //by tallest 0407
1760 static void
1761 kill_ppp(int ppp_num)
1763 #ifdef MPPPOE_SUPPORT
1764 char buf[500];
1766 if(demand)
1768 sprintf(buf,"%.500s %d",ppp_disconnect_func,ppp_num);
1769 info("tallest:=====(Executing external command - %s)=====\n",buf);
1770 system(buf);
1772 #endif
1775 //=============================================================================
1778 * ipcp_down - IPCP has gone DOWN.
1780 * Take the IP network interface down, clear its addresses
1781 * and delete routes through it.
1783 static void
1784 ipcp_down(f)
1785 fsm *f;
1787 IPCPDEBUG(("ipcp: down"));
1788 update_link_stats(f->unit);
1789 if (ip_down_hook)
1790 ip_down_hook();
1791 if (ipcp_is_up) {
1792 ipcp_is_up = 0;
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).
1801 if (demand) {
1802 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1803 } else {
1804 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1805 sifdown(f->unit);
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
1816 #endif
1822 * ipcp_clear_addrs() - clear the interface addresses, routes,
1823 * proxy arp entries, etc.
1825 static void
1826 ipcp_clear_addrs(unit, ouraddr, hisaddr)
1827 int unit;
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.
1846 static void
1847 ipcp_finished(f)
1848 fsm *f;
1850 np_finished(f->unit, PPP_IP);
1855 * ipcp_script_done - called when the ip-up or ip-down script
1856 * has finished.
1858 static void
1859 ipcp_script_done(arg)
1860 void *arg;
1862 ipcp_script_pid = 0;
1863 switch (ipcp_script_state) {
1864 case s_up:
1865 if (ipcp_fsm[0].state != OPENED) {
1866 ipcp_script_state = s_down;
1867 ipcp_script(_PATH_IPDOWN);
1869 break;
1870 case s_down:
1871 if (ipcp_fsm[0].state == OPENED) {
1872 ipcp_script_state = s_up;
1873 ipcp_script(_PATH_IPUP);
1875 break;
1881 * ipcp_script - Execute a script with arguments
1882 * interface-name tty-name speed local-IP remote-IP.
1884 static void
1885 ipcp_script(script)
1886 char *script;
1888 char strspeed[32], strlocal[32], strremote[32];
1889 char *argv[8];
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);
1895 argv[0] = script;
1896 argv[1] = ifname;
1897 argv[2] = devnam;
1898 argv[3] = strspeed;
1899 argv[4] = strlocal;
1900 argv[5] = strremote;
1901 argv[6] = ipparam;
1902 argv[7] = NULL;
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 )
1911 static unsigned int
1912 get_lan_ip( void ) // tallest 0129
1914 struct ifreq ifr;
1915 int timeout, s;
1917 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
1918 return;
1919 strncpy(ifr.ifr_name, "br0", IFNAMSIZ);
1921 timeout = 3;
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) );
1929 #endif
1932 * create_resolv - create the replacement resolv.conf file
1934 static void
1935 create_resolv(peerdns1, peerdns2)
1936 u_int32_t peerdns1, peerdns2;
1938 FILE *f;
1940 f = fopen(_PATH_RESOLV, "w");
1941 if (f == NULL) {
1942 error("Failed to create %s: %m", _PATH_RESOLV);
1943 return;
1946 if (peerdns1)
1947 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
1949 if (peerdns2)
1950 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
1952 if (ferror(f))
1953 error("Write failed to %s: %m", _PATH_RESOLV);
1955 fclose(f);
1959 * ipcp_printpkt - print the contents of an IPCP packet.
1961 static char *ipcp_codenames[] = {
1962 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1963 "TermReq", "TermAck", "CodeRej"
1966 static int
1967 ipcp_printpkt(p, plen, printer, arg)
1968 u_char *p;
1969 int plen;
1970 void (*printer) __P((void *, char *, ...));
1971 void *arg;
1973 int code, id, len, olen;
1974 u_char *pstart, *optend;
1975 u_short cishort;
1976 u_int32_t cilong;
1978 if (plen < HEADERLEN)
1979 return 0;
1980 pstart = p;
1981 GETCHAR(code, p);
1982 GETCHAR(id, p);
1983 GETSHORT(len, p);
1984 if (len < HEADERLEN || len > plen)
1985 return 0;
1987 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1988 printer(arg, " %s", ipcp_codenames[code-1]);
1989 else
1990 printer(arg, " code=0x%x", code);
1991 printer(arg, " id=0x%x", id);
1992 len -= HEADERLEN;
1993 switch (code) {
1994 case CONFREQ:
1995 case CONFACK:
1996 case CONFNAK:
1997 case CONFREJ:
1998 /* print option list */
1999 while (len >= 2) {
2000 GETCHAR(code, p);
2001 GETCHAR(olen, p);
2002 p -= 2;
2003 if (olen < 2 || olen > len) {
2004 break;
2006 printer(arg, " <");
2007 len -= olen;
2008 optend = p + olen;
2009 switch (code) {
2010 case CI_ADDRS:
2011 if (olen == CILEN_ADDRS) {
2012 p += 2;
2013 GETLONG(cilong, p);
2014 printer(arg, "addrs %I", htonl(cilong));
2015 GETLONG(cilong, p);
2016 printer(arg, " %I", htonl(cilong));
2018 break;
2019 case CI_COMPRESSTYPE:
2020 if (olen >= CILEN_COMPRESS) {
2021 p += 2;
2022 GETSHORT(cishort, p);
2023 printer(arg, "compress ");
2024 switch (cishort) {
2025 case IPCP_VJ_COMP:
2026 printer(arg, "VJ");
2027 break;
2028 case IPCP_VJ_COMP_OLD:
2029 printer(arg, "old-VJ");
2030 break;
2031 default:
2032 printer(arg, "0x%x", cishort);
2035 break;
2036 case CI_ADDR:
2037 if (olen == CILEN_ADDR) {
2038 p += 2;
2039 GETLONG(cilong, p);
2040 printer(arg, "addr %I", htonl(cilong));
2042 break;
2043 case CI_MS_DNS1:
2044 case CI_MS_DNS2:
2045 p += 2;
2046 GETLONG(cilong, p);
2047 printer(arg, "ms-dns%d %I", code - CI_MS_DNS1 + 1,
2048 htonl(cilong));
2049 break;
2050 case CI_MS_WINS1:
2051 case CI_MS_WINS2:
2052 p += 2;
2053 GETLONG(cilong, p);
2054 printer(arg, "ms-wins %I", htonl(cilong));
2055 break;
2057 while (p < optend) {
2058 GETCHAR(code, p);
2059 printer(arg, " %.2x", code);
2061 printer(arg, ">");
2063 break;
2065 case TERMACK:
2066 case TERMREQ:
2067 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2068 printer(arg, " ");
2069 print_string((char *)p, len, printer, arg);
2070 p += len;
2071 len = 0;
2073 break;
2076 /* print the rest of the bytes in the packet */
2077 for (; len > 0; --len) {
2078 GETCHAR(code, p);
2079 printer(arg, " %.2x", code);
2082 return p - pstart;
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
2088 * with no data.
2090 #define IP_HDRLEN 20 /* bytes */
2091 #define IP_OFFMASK 0x1fff
2092 // #define IPPROTO_TCP 6
2093 #define TCP_HDRLEN 20
2094 #define TH_FIN 0x01
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])
2108 static int
2109 ip_active_pkt(pkt, len)
2110 u_char *pkt;
2111 int len;
2113 u_char *tcp;
2114 int hlen;
2116 len -= PPP_HDRLEN;
2117 pkt += PPP_HDRLEN;
2118 if (len < IP_HDRLEN)
2119 return 0;
2120 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2121 return 0;
2122 if (get_ipproto(pkt) != IPPROTO_TCP)
2123 return 1;
2124 hlen = get_iphl(pkt) * 4;
2125 if (len < hlen + TCP_HDRLEN)
2126 return 0;
2127 tcp = pkt + hlen;
2128 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)
2129 return 0;
2130 return 1;