Deleted Added
sdiff udiff text old ( 31171 ) new ( 31272 )
full compact
1/*
2 * PPP IP Control Protocol (IPCP) Module
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: ipcp.c,v 1.36 1997/11/14 15:39:14 brian Exp $
21 *
22 * TODO:
23 * o More RFC1772 backwoard compatibility
24 */
25#include <sys/param.h>
26#include <netinet/in_systm.h>
27#include <netinet/in.h>
28#include <netinet/ip.h>
29#include <arpa/inet.h>
30#include <sys/socket.h>
31#include <netdb.h>
32
33#include <limits.h>
34#include <stdio.h>
35#include <string.h>
36#include <time.h>
37#include <unistd.h>
38
39#include "mbuf.h"
40#include "log.h"
41#include "defs.h"
42#include "timer.h"
43#include "fsm.h"
44#include "lcpproto.h"
45#include "lcp.h"
46#include "ipcp.h"
47#include "slcompress.h"
48#include "os.h"
49#include "phase.h"
50#include "loadalias.h"
51#include "command.h"
52#include "vars.h"
53#include "vjcomp.h"
54#include "ip.h"
55#include "throughput.h"
56
57#ifndef NOMSEXT
58struct in_addr ns_entries[2];
59struct in_addr nbns_entries[2];
60#endif
61
62struct ipcpstate IpcpInfo;
63struct in_range DefMyAddress;
64struct in_range DefHisAddress;
65struct in_addr TriggerAddress;
66int HaveTriggerAddress;
67
68static void IpcpSendConfigReq(struct fsm *);
69static void IpcpSendTerminateAck(struct fsm *);
70static void IpcpSendTerminateReq(struct fsm *);
71static void IpcpDecodeConfig(u_char *, int, int);
72static void IpcpLayerStart(struct fsm *);
73static void IpcpLayerFinish(struct fsm *);
74static void IpcpLayerUp(struct fsm *);
75static void IpcpLayerDown(struct fsm *);
76static void IpcpInitRestartCounter(struct fsm *);
77
78#define REJECTED(p, x) (p->his_reject & (1<<x))
79
80struct fsm IpcpFsm = {
81 "IPCP",
82 PROTO_IPCP,
83 IPCP_MAXCODE,
84 OPEN_ACTIVE,
85 ST_INITIAL,
86 0, 0, 0,
87
88 0,
89 {0, 0, 0, NULL, NULL, NULL},
90 {0, 0, 0, NULL, NULL, NULL},
91 LogIPCP,
92
93 IpcpLayerUp,
94 IpcpLayerDown,
95 IpcpLayerStart,
96 IpcpLayerFinish,
97 IpcpInitRestartCounter,
98 IpcpSendConfigReq,
99 IpcpSendTerminateReq,
100 IpcpSendTerminateAck,
101 IpcpDecodeConfig,
102};
103
104static char *cftypes[] = {
105 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
106 "???",
107 "IPADDRS", /* 1: IP-Addresses */ /* deprecated */
108 "COMPPROTO", /* 2: IP-Compression-Protocol */
109 "IPADDR", /* 3: IP-Address */
110};
111
112#define NCFTYPES (sizeof(cftypes)/sizeof(char *))
113
114static char *cftypes128[] = {
115 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
116 "???",
117 "PRIDNS", /* 129: Primary DNS Server Address */
118 "PRINBNS", /* 130: Primary NBNS Server Address */
119 "SECDNS", /* 131: Secondary DNS Server Address */
120 "SECNBNS", /* 132: Secondary NBNS Server Address */
121};
122
123#define NCFTYPES128 (sizeof(cftypes)/sizeof(char *))
124
125struct pppThroughput throughput;
126
127void
128IpcpAddInOctets(int n)
129{
130 throughput_addin(&throughput, n);
131}
132
133void
134IpcpAddOutOctets(int n)
135{
136 throughput_addout(&throughput, n);
137}
138
139int
140ReportIpcpStatus()
141{
142 struct ipcpstate *icp = &IpcpInfo;
143 struct fsm *fp = &IpcpFsm;
144
145 if (!VarTerm)
146 return 1;
147 fprintf(VarTerm, "%s [%s]\n", fp->name, StateNames[fp->state]);
148 fprintf(VarTerm, " his side: %s, %lx\n",
149 inet_ntoa(icp->his_ipaddr), icp->his_compproto);
150 fprintf(VarTerm, " my side: %s, %lx\n",
151 inet_ntoa(icp->want_ipaddr), icp->want_compproto);
152
153 fprintf(VarTerm, "Defaults:\n");
154 fprintf(VarTerm, " My Address: %s/%d\n",
155 inet_ntoa(DefMyAddress.ipaddr), DefMyAddress.width);
156 fprintf(VarTerm, " His Address: %s/%d\n",
157 inet_ntoa(DefHisAddress.ipaddr), DefHisAddress.width);
158 if (HaveTriggerAddress)
159 fprintf(VarTerm, " Negotiation(trigger): %s\n", inet_ntoa(TriggerAddress));
160 else
161 fprintf(VarTerm, " Negotiation(trigger): MYADDR\n");
162
163 fprintf(VarTerm, "\n");
164 throughput_disp(&throughput, VarTerm);
165
166 return 0;
167}
168
169void
170IpcpDefAddress()
171{
172 struct hostent *hp;
173 char name[200];
174
175 memset(&DefMyAddress, '\0', sizeof(DefMyAddress));
176 memset(&DefHisAddress, '\0', sizeof(DefHisAddress));
177 TriggerAddress.s_addr = 0;
178 HaveTriggerAddress = 0;
179 if (gethostname(name, sizeof(name)) == 0) {
180 hp = gethostbyname(name);
181 if (hp && hp->h_addrtype == AF_INET) {
182 memcpy(&DefMyAddress.ipaddr.s_addr, hp->h_addr, hp->h_length);
183 }
184 }
185}
186
187void
188IpcpInit()
189{
190 struct ipcpstate *icp = &IpcpInfo;
191
192 FsmInit(&IpcpFsm);
193 memset(icp, '\0', sizeof(struct ipcpstate));
194 if ((mode & MODE_DEDICATED) && !GetLabel()) {
195 icp->want_ipaddr.s_addr = icp->his_ipaddr.s_addr = 0;
196 } else {
197 icp->want_ipaddr.s_addr = DefMyAddress.ipaddr.s_addr;
198 icp->his_ipaddr.s_addr = DefHisAddress.ipaddr.s_addr;
199 }
200
201 /*
202 * Some implementations of PPP require that we send a
203 * *special* value as our address, even though the rfc specifies
204 * full negotiation (e.g. "0.0.0.0" or Not "0.0.0.0").
205 */
206 if (HaveTriggerAddress) {
207 icp->want_ipaddr.s_addr = TriggerAddress.s_addr;
208 LogPrintf(LogIPCP, "Using trigger address %s\n", inet_ntoa(TriggerAddress));
209 }
210 if (Enabled(ConfVjcomp))
211 icp->want_compproto = (PROTO_VJCOMP << 16) | ((MAX_STATES - 1) << 8) | 1;
212 else
213 icp->want_compproto = 0;
214 icp->heis1172 = 0;
215 IpcpFsm.maxconfig = 10;
216 throughput_init(&throughput);
217}
218
219static void
220IpcpInitRestartCounter(struct fsm * fp)
221{
222 fp->FsmTimer.load = VarRetryTimeout * SECTICKS;
223 fp->restart = 5;
224}
225
226static void
227IpcpSendConfigReq(struct fsm * fp)
228{
229 u_char *cp;
230 struct ipcpstate *icp = &IpcpInfo;
231
232 cp = ReqBuff;
233 LogPrintf(LogIPCP, "IpcpSendConfigReq\n");
234 if (!DEV_IS_SYNC || !REJECTED(icp, TY_IPADDR))
235 PutConfValue(LogIPCP, &cp, cftypes, TY_IPADDR, 6,
236 ntohl(icp->want_ipaddr.s_addr));
237 if (icp->want_compproto && !REJECTED(icp, TY_COMPPROTO)) {
238 if (icp->heis1172)
239 PutConfValue(LogIPCP, &cp, cftypes, TY_COMPPROTO, 4,
240 icp->want_compproto >> 16);
241 else
242 PutConfValue(LogIPCP, &cp, cftypes, TY_COMPPROTO, 6, icp->want_compproto);
243 }
244 FsmOutput(fp, CODE_CONFIGREQ, fp->reqid++, ReqBuff, cp - ReqBuff);
245}
246
247static void
248IpcpSendTerminateReq(struct fsm * fp)
249{
250 /* XXX: No code yet */
251}
252
253static void
254IpcpSendTerminateAck(struct fsm * fp)
255{
256 LogPrintf(LogIPCP, "IpcpSendTerminateAck\n");
257 FsmOutput(fp, CODE_TERMACK, fp->reqid++, NULL, 0);
258}
259
260static void
261IpcpLayerStart(struct fsm * fp)
262{
263 LogPrintf(LogIPCP, "IpcpLayerStart.\n");
264}
265
266static void
267IpcpLayerFinish(struct fsm * fp)
268{
269 LogPrintf(LogIPCP, "IpcpLayerFinish.\n");
270 reconnect(RECON_FALSE);
271 LcpClose();
272 NewPhase(PHASE_TERMINATE);
273}
274
275static void
276IpcpLayerDown(struct fsm * fp)
277{
278 LogPrintf(LogIPCP, "IpcpLayerDown.\n");
279 throughput_stop(&throughput);
280 throughput_log(&throughput, LogIPCP, NULL);
281}
282
283/*
284 * Called when IPCP has reached to OPEN state
285 */
286static void
287IpcpLayerUp(struct fsm * fp)
288{
289 char tbuff[100];
290
291 Prompt();
292 LogPrintf(LogIPCP, "IpcpLayerUp(%d).\n", fp->state);
293 snprintf(tbuff, sizeof(tbuff), "myaddr = %s ",
294 inet_ntoa(IpcpInfo.want_ipaddr));
295
296 if (IpcpInfo.his_compproto >> 16 == PROTO_VJCOMP)
297 VjInit((IpcpInfo.his_compproto >> 8) & 255);
298
299 LogPrintf(LogIsKept(LogIPCP) ? LogIPCP : LogLINK, " %s hisaddr = %s\n",
300 tbuff, inet_ntoa(IpcpInfo.his_ipaddr));
301 if (OsSetIpaddress(IpcpInfo.want_ipaddr, IpcpInfo.his_ipaddr, ifnetmask) < 0) {
302 if (VarTerm)
303 LogPrintf(LogERROR, "IpcpLayerUp: unable to set ip address\n");
304 return;
305 }
306 if (mode & MODE_ALIAS)
307 VarPacketAliasSetAddress(IpcpInfo.want_ipaddr);
308 OsLinkup();
309 throughput_start(&throughput);
310 StartIdleTimer();
311}
312
313void
314IpcpUp()
315{
316 FsmUp(&IpcpFsm);
317 LogPrintf(LogIPCP, "IPCP Up event!!\n");
318}
319
320void
321IpcpOpen()
322{
323 FsmOpen(&IpcpFsm);
324}
325
326static int
327AcceptableAddr(struct in_range * prange, struct in_addr ipaddr)
328{
329 LogPrintf(LogDEBUG, "requested = %x\n", htonl(ipaddr.s_addr));
330 LogPrintf(LogDEBUG, "range = %x\n", htonl(prange->ipaddr.s_addr));
331 LogPrintf(LogDEBUG, "/%x\n", htonl(prange->mask.s_addr));
332 LogPrintf(LogDEBUG, "%x, %x\n", htonl(prange->ipaddr.s_addr & prange->
333 mask.s_addr), htonl(ipaddr.s_addr & prange->mask.s_addr));
334 return (prange->ipaddr.s_addr & prange->mask.s_addr) ==
335 (ipaddr.s_addr & prange->mask.s_addr) && ipaddr.s_addr;
336}
337
338static void
339IpcpDecodeConfig(u_char * cp, int plen, int mode_type)
340{
341 int type, length;
342 u_long *lp, compproto;
343 struct compreq *pcomp;
344 struct in_addr ipaddr, dstipaddr, dnsstuff, ms_info_req;
345 char tbuff[100];
346 char tbuff2[100];
347
348 ackp = AckBuff;
349 nakp = NakBuff;
350 rejp = RejBuff;
351
352 while (plen >= sizeof(struct fsmconfig)) {
353 type = *cp;
354 length = cp[1];
355 if (type < NCFTYPES)
356 snprintf(tbuff, sizeof(tbuff), " %s[%d] ", cftypes[type], length);
357 else if (type > 128 && type < 128 + NCFTYPES128)
358 snprintf(tbuff, sizeof(tbuff), " %s[%d] ", cftypes128[type], length);
359 else
360 snprintf(tbuff, sizeof(tbuff), " ??? ");
361
362 switch (type) {
363 case TY_IPADDR: /* RFC1332 */
364 lp = (u_long *) (cp + 2);
365 ipaddr.s_addr = *lp;
366 LogPrintf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
367
368 switch (mode_type) {
369 case MODE_REQ:
370 if (!AcceptableAddr(&DefHisAddress, ipaddr)) {
371 /*
372 * If destination address is not acceptable, insist to use what we
373 * want to use.
374 */
375 memcpy(nakp, cp, 2);
376 memcpy(nakp+2, &IpcpInfo.his_ipaddr.s_addr, length);
377 nakp += length;
378 break;
379 }
380 IpcpInfo.his_ipaddr = ipaddr;
381 memcpy(ackp, cp, length);
382 ackp += length;
383 break;
384 case MODE_NAK:
385 if (AcceptableAddr(&DefMyAddress, ipaddr)) {
386
387 /*
388 * Use address suggested by peer.
389 */
390 snprintf(tbuff2, sizeof(tbuff2), "%s changing address: %s ", tbuff,
391 inet_ntoa(IpcpInfo.want_ipaddr));
392 LogPrintf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr));
393 IpcpInfo.want_ipaddr = ipaddr;
394 }
395 break;
396 case MODE_REJ:
397 IpcpInfo.his_reject |= (1 << type);
398 break;
399 }
400 break;
401 case TY_COMPPROTO:
402 lp = (u_long *) (cp + 2);
403 compproto = htonl(*lp);
404 LogPrintf(LogIPCP, "%s %08x\n", tbuff, compproto);
405
406 switch (mode_type) {
407 case MODE_REQ:
408 if (!Acceptable(ConfVjcomp)) {
409 memcpy(rejp, cp, length);
410 rejp += length;
411 } else {
412 pcomp = (struct compreq *) (cp + 2);
413 switch (length) {
414 case 4: /* RFC1172 */
415 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
416 LogPrintf(LogWARN, "Peer is speaking RFC1172 compression protocol !\n");
417 IpcpInfo.heis1172 = 1;
418 IpcpInfo.his_compproto = compproto;
419 memcpy(ackp, cp, length);
420 ackp += length;
421 } else {
422 memcpy(nakp, cp, 2);
423 pcomp->proto = htons(PROTO_VJCOMP);
424 memcpy(nakp+2, &pcomp, 2);
425 nakp += length;
426 }
427 break;
428 case 6: /* RFC1332 */
429 if (ntohs(pcomp->proto) == PROTO_VJCOMP
430 && pcomp->slots < MAX_STATES && pcomp->slots > 2) {
431 IpcpInfo.his_compproto = compproto;
432 IpcpInfo.heis1172 = 0;
433 memcpy(ackp, cp, length);
434 ackp += length;
435 } else {
436 memcpy(nakp, cp, 2);
437 pcomp->proto = htons(PROTO_VJCOMP);
438 pcomp->slots = MAX_STATES - 1;
439 pcomp->compcid = 0;
440 memcpy(nakp+2, &pcomp, sizeof(pcomp));
441 nakp += length;
442 }
443 break;
444 default:
445 memcpy(rejp, cp, length);
446 rejp += length;
447 break;
448 }
449 }
450 break;
451 case MODE_NAK:
452 LogPrintf(LogIPCP, "%s changing compproto: %08x --> %08x\n",
453 tbuff, IpcpInfo.want_compproto, compproto);
454 IpcpInfo.want_compproto = compproto;
455 break;
456 case MODE_REJ:
457 IpcpInfo.his_reject |= (1 << type);
458 break;
459 }
460 break;
461 case TY_IPADDRS: /* RFC1172 */
462 lp = (u_long *) (cp + 2);
463 ipaddr.s_addr = *lp;
464 lp = (u_long *) (cp + 6);
465 dstipaddr.s_addr = *lp;
466 snprintf(tbuff2, sizeof(tbuff2), "%s %s,", tbuff, inet_ntoa(ipaddr));
467 LogPrintf(LogIPCP, "%s %s\n", tbuff2, inet_ntoa(dstipaddr));
468
469 switch (mode_type) {
470 case MODE_REQ:
471 IpcpInfo.his_ipaddr = ipaddr;
472 IpcpInfo.want_ipaddr = dstipaddr;
473 memcpy(ackp, cp, length);
474 ackp += length;
475 break;
476 case MODE_NAK:
477 snprintf(tbuff2, sizeof(tbuff2), "%s changing address: %s", tbuff,
478 inet_ntoa(IpcpInfo.want_ipaddr));
479 LogPrintf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr));
480 IpcpInfo.want_ipaddr = ipaddr;
481 IpcpInfo.his_ipaddr = dstipaddr;
482 break;
483 case MODE_REJ:
484 IpcpInfo.his_reject |= (1 << type);
485 break;
486 }
487 break;
488
489 /*
490 * MS extensions for MS's PPP
491 */
492
493#ifndef NOMSEXT
494 case TY_PRIMARY_DNS: /* MS PPP DNS negotiation hack */
495 case TY_SECONDARY_DNS:
496 if (!Enabled(ConfMSExt)) {
497 LogPrintf(LogIPCP, "MS NS req - rejected - msext disabled\n");
498 IpcpInfo.my_reject |= (1 << type);
499 memcpy(rejp, cp, length);
500 rejp += length;
501 break;
502 }
503 switch (mode_type) {
504 case MODE_REQ:
505 lp = (u_long *) (cp + 2);
506 dnsstuff.s_addr = *lp;
507 ms_info_req.s_addr = ns_entries[((type - TY_PRIMARY_DNS) ? 1 : 0)].s_addr;
508 if (dnsstuff.s_addr != ms_info_req.s_addr) {
509
510 /*
511 * So the client has got the DNS stuff wrong (first request) so
512 * we'll tell 'em how it is
513 */
514 memcpy(nakp, cp, 2); /* copy first two (type/length) */
515 LogPrintf(LogIPCP, "MS NS req %d:%s->%s - nak\n",
516 type,
517 inet_ntoa(dnsstuff),
518 inet_ntoa(ms_info_req));
519 memcpy(nakp+2, &ms_info_req, length);
520 nakp += length;
521 break;
522 }
523
524 /*
525 * Otherwise they have it right (this time) so we send a ack packet
526 * back confirming it... end of story
527 */
528 LogPrintf(LogIPCP, "MS NS req %d:%s ok - ack\n",
529 type,
530 inet_ntoa(ms_info_req));
531 memcpy(ackp, cp, length);
532 ackp += length;
533 break;
534 case MODE_NAK: /* what does this mean?? */
535 LogPrintf(LogIPCP, "MS NS req %d - NAK??\n", type);
536 break;
537 case MODE_REJ: /* confused?? me to :) */
538 LogPrintf(LogIPCP, "MS NS req %d - REJ??\n", type);
539 break;
540 }
541 break;
542
543 case TY_PRIMARY_NBNS: /* MS PPP NetBIOS nameserver hack */
544 case TY_SECONDARY_NBNS:
545 if (!Enabled(ConfMSExt)) {
546 LogPrintf(LogIPCP, "MS NBNS req - rejected - msext disabled\n");
547 IpcpInfo.my_reject |= (1 << type);
548 memcpy(rejp, cp, length);
549 rejp += length;
550 break;
551 }
552 switch (mode_type) {
553 case MODE_REQ:
554 lp = (u_long *) (cp + 2);
555 dnsstuff.s_addr = *lp;
556 ms_info_req.s_addr = nbns_entries[((type - TY_PRIMARY_NBNS) ? 1 : 0)].s_addr;
557 if (dnsstuff.s_addr != ms_info_req.s_addr) {
558 memcpy(nakp, cp, 2);
559 memcpy(nakp+2, &ms_info_req.s_addr, length);
560 LogPrintf(LogIPCP, "MS NBNS req %d:%s->%s - nak\n",
561 type,
562 inet_ntoa(dnsstuff),
563 inet_ntoa(ms_info_req));
564 nakp += length;
565 break;
566 }
567 LogPrintf(LogIPCP, "MS NBNS req %d:%s ok - ack\n",
568 type,
569 inet_ntoa(ms_info_req));
570 memcpy(ackp, cp, length);
571 ackp += length;
572 break;
573 case MODE_NAK:
574 LogPrintf(LogIPCP, "MS NBNS req %d - NAK??\n", type);
575 break;
576 case MODE_REJ:
577 LogPrintf(LogIPCP, "MS NBNS req %d - REJ??\n", type);
578 break;
579 }
580 break;
581
582#endif
583
584 default:
585 IpcpInfo.my_reject |= (1 << type);
586 memcpy(rejp, cp, length);
587 rejp += length;
588 break;
589 }
590 plen -= length;
591 cp += length;
592 }
593}
594
595void
596IpcpInput(struct mbuf * bp)
597{
598 FsmInput(&IpcpFsm, bp);
599}