CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / ppp / pppd / ipxcp.c
blob7c39397e19a4daf72f083b27e4f67f5fdb00778b
1 /*
2 * ipxcp.c - PPP IPX 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 #ifdef IPX_CHANGE
22 #define RCSID "$Id: ipxcp.c,v 1.1.1.4 2003/10/14 08:09:53 sparq Exp $"
25 * TODO:
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include "pppd.h"
37 #include "fsm.h"
38 #include "ipxcp.h"
39 #include "pathnames.h"
40 #include "magic.h"
42 static const char rcsid[] = RCSID;
44 /* global vars */
45 ipxcp_options ipxcp_wantoptions[NUM_PPP]; /* Options that we want to request */
46 ipxcp_options ipxcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
47 ipxcp_options ipxcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
48 ipxcp_options ipxcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
50 #define wo (&ipxcp_wantoptions[0])
51 #define ao (&ipxcp_allowoptions[0])
52 #define go (&ipxcp_gotoptions[0])
53 #define ho (&ipxcp_hisoptions[0])
56 * Callbacks for fsm code. (CI = Configuration Information)
58 static void ipxcp_resetci __P((fsm *)); /* Reset our CI */
59 static int ipxcp_cilen __P((fsm *)); /* Return length of our CI */
60 static void ipxcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
61 static int ipxcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
62 static int ipxcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
63 static int ipxcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
64 static int ipxcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
65 static void ipxcp_up __P((fsm *)); /* We're UP */
66 static void ipxcp_down __P((fsm *)); /* We're DOWN */
67 static void ipxcp_finished __P((fsm *)); /* Don't need lower layer */
68 static void ipxcp_script __P((fsm *, char *)); /* Run an up/down script */
70 fsm ipxcp_fsm[NUM_PPP]; /* IPXCP fsm structure */
72 static fsm_callbacks ipxcp_callbacks = { /* IPXCP callback routines */
73 ipxcp_resetci, /* Reset our Configuration Information */
74 ipxcp_cilen, /* Length of our Configuration Information */
75 ipxcp_addci, /* Add our Configuration Information */
76 ipxcp_ackci, /* ACK our Configuration Information */
77 ipxcp_nakci, /* NAK our Configuration Information */
78 ipxcp_rejci, /* Reject our Configuration Information */
79 ipxcp_reqci, /* Request peer's Configuration Information */
80 ipxcp_up, /* Called when fsm reaches OPENED state */
81 ipxcp_down, /* Called when fsm leaves OPENED state */
82 NULL, /* Called when we want the lower layer up */
83 ipxcp_finished, /* Called when we want the lower layer down */
84 NULL, /* Called when Protocol-Reject received */
85 NULL, /* Retransmission is necessary */
86 NULL, /* Called to handle protocol-specific codes */
87 "IPXCP" /* String name of protocol */
91 * Command-line options.
93 static int setipxnode __P((char **));
94 static void printipxnode __P((option_t *,
95 void (*)(void *, char *, ...), void *));
96 static int setipxname __P((char **));
98 static option_t ipxcp_option_list[] = {
99 { "ipx", o_bool, &ipxcp_protent.enabled_flag,
100 "Enable IPXCP (and IPX)", OPT_PRIO | 1 },
101 { "+ipx", o_bool, &ipxcp_protent.enabled_flag,
102 "Enable IPXCP (and IPX)", OPT_PRIOSUB | OPT_ALIAS | 1 },
103 { "noipx", o_bool, &ipxcp_protent.enabled_flag,
104 "Disable IPXCP (and IPX)", OPT_PRIOSUB },
105 { "-ipx", o_bool, &ipxcp_protent.enabled_flag,
106 "Disable IPXCP (and IPX)", OPT_PRIOSUB | OPT_ALIAS },
108 { "ipx-network", o_uint32, &ipxcp_wantoptions[0].our_network,
109 "Set our IPX network number", OPT_PRIO, &ipxcp_wantoptions[0].neg_nn },
111 { "ipxcp-accept-network", o_bool, &ipxcp_wantoptions[0].accept_network,
112 "Accept peer IPX network number", 1,
113 &ipxcp_allowoptions[0].accept_network },
115 { "ipx-node", o_special, (void *)setipxnode,
116 "Set IPX node number", OPT_A2PRINTER, (void *)printipxnode },
118 { "ipxcp-accept-local", o_bool, &ipxcp_wantoptions[0].accept_local,
119 "Accept our IPX address", 1,
120 &ipxcp_allowoptions[0].accept_local },
122 { "ipxcp-accept-remote", o_bool, &ipxcp_wantoptions[0].accept_remote,
123 "Accept peer's IPX address", 1,
124 &ipxcp_allowoptions[0].accept_remote },
126 { "ipx-routing", o_int, &ipxcp_wantoptions[0].router,
127 "Set IPX routing proto number", OPT_PRIO,
128 &ipxcp_wantoptions[0].neg_router },
130 { "ipx-router-name", o_special, setipxname,
131 "Set IPX router name", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC,
132 &ipxcp_wantoptions[0].name },
134 { "ipxcp-restart", o_int, &ipxcp_fsm[0].timeouttime,
135 "Set timeout for IPXCP", OPT_PRIO },
136 { "ipxcp-max-terminate", o_int, &ipxcp_fsm[0].maxtermtransmits,
137 "Set max #xmits for IPXCP term-reqs", OPT_PRIO },
138 { "ipxcp-max-configure", o_int, &ipxcp_fsm[0].maxconfreqtransmits,
139 "Set max #xmits for IPXCP conf-reqs", OPT_PRIO },
140 { "ipxcp-max-failure", o_int, &ipxcp_fsm[0].maxnakloops,
141 "Set max #conf-naks for IPXCP", OPT_PRIO },
143 { NULL }
147 * Protocol entry points.
150 static void ipxcp_init __P((int));
151 static void ipxcp_open __P((int));
152 static void ipxcp_close __P((int, char *));
153 static void ipxcp_lowerup __P((int));
154 static void ipxcp_lowerdown __P((int));
155 static void ipxcp_input __P((int, u_char *, int));
156 static void ipxcp_protrej __P((int));
157 static int ipxcp_printpkt __P((u_char *, int,
158 void (*) __P((void *, char *, ...)), void *));
160 struct protent ipxcp_protent = {
161 PPP_IPXCP,
162 ipxcp_init,
163 ipxcp_input,
164 ipxcp_protrej,
165 ipxcp_lowerup,
166 ipxcp_lowerdown,
167 ipxcp_open,
168 ipxcp_close,
169 ipxcp_printpkt,
170 NULL,
172 "IPXCP",
173 "IPX",
174 ipxcp_option_list,
175 NULL,
176 NULL,
177 NULL
181 * Lengths of configuration options.
184 #define CILEN_VOID 2
185 #define CILEN_COMPLETE 2 /* length of complete option */
186 #define CILEN_NETN 6 /* network number length option */
187 #define CILEN_NODEN 8 /* node number length option */
188 #define CILEN_PROTOCOL 4 /* Minimum length of routing protocol */
189 #define CILEN_NAME 3 /* Minimum length of router name */
190 #define CILEN_COMPRESS 4 /* Minimum length of compression protocol */
192 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
193 (x) == CONFNAK ? "NAK" : "REJ")
195 static int ipxcp_is_up;
197 static char *ipx_ntoa __P((u_int32_t));
199 /* Used in printing the node number */
200 #define NODE(base) base[0], base[1], base[2], base[3], base[4], base[5]
202 /* Used to generate the proper bit mask */
203 #define BIT(num) (1 << (num))
206 * Convert from internal to external notation
209 static short int
210 to_external(internal)
211 short int internal;
213 short int external;
215 if (internal & BIT(IPX_NONE) )
216 external = IPX_NONE;
217 else
218 external = RIP_SAP;
220 return external;
224 * Make a string representation of a network IP address.
227 static char *
228 ipx_ntoa(ipxaddr)
229 u_int32_t ipxaddr;
231 static char b[64];
232 slprintf(b, sizeof(b), "%x", ipxaddr);
233 return b;
237 static u_char *
238 setipxnodevalue(src,dst)
239 u_char *src, *dst;
241 int indx;
242 int item;
244 for (;;) {
245 if (!isxdigit (*src))
246 break;
248 for (indx = 0; indx < 5; ++indx) {
249 dst[indx] <<= 4;
250 dst[indx] |= (dst[indx + 1] >> 4) & 0x0F;
253 item = toupper (*src) - '0';
254 if (item > 9)
255 item -= 7;
257 dst[5] = (dst[5] << 4) | item;
258 ++src;
260 return src;
263 static int ipx_prio_our, ipx_prio_his;
265 static int
266 setipxnode(argv)
267 char **argv;
269 char *end;
270 int have_his = 0;
271 u_char our_node[6];
272 u_char his_node[6];
274 memset (our_node, 0, 6);
275 memset (his_node, 0, 6);
277 end = setipxnodevalue (*argv, our_node);
278 if (*end == ':') {
279 have_his = 1;
280 end = setipxnodevalue (++end, his_node);
283 if (*end == '\0') {
284 ipxcp_wantoptions[0].neg_node = 1;
285 if (option_priority >= ipx_prio_our) {
286 memcpy(&ipxcp_wantoptions[0].our_node[0], our_node, 6);
287 ipx_prio_our = option_priority;
289 if (have_his && option_priority >= ipx_prio_his) {
290 memcpy(&ipxcp_wantoptions[0].his_node[0], his_node, 6);
291 ipx_prio_his = option_priority;
293 return 1;
296 option_error("invalid parameter '%s' for ipx-node option", *argv);
297 return 0;
300 static void
301 printipxnode(opt, printer, arg)
302 option_t *opt;
303 void (*printer) __P((void *, char *, ...));
304 void *arg;
306 unsigned char *p;
308 p = ipxcp_wantoptions[0].our_node;
309 if (ipx_prio_our)
310 printer(arg, "%.2x%.2x%.2x%.2x%.2x%.2x",
311 p[0], p[1], p[2], p[3], p[4], p[5]);
312 printer(arg, ":");
313 p = ipxcp_wantoptions[0].his_node;
314 if (ipx_prio_his)
315 printer(arg, "%.2x%.2x%.2x%.2x%.2x%.2x",
316 p[0], p[1], p[2], p[3], p[4], p[5]);
319 static int
320 setipxname (argv)
321 char **argv;
323 char *dest = ipxcp_wantoptions[0].name;
324 char *src = *argv;
325 int count;
326 char ch;
328 ipxcp_wantoptions[0].neg_name = 1;
329 ipxcp_allowoptions[0].neg_name = 1;
330 memset (dest, '\0', sizeof (ipxcp_wantoptions[0].name));
332 count = 0;
333 while (*src) {
334 ch = *src++;
335 if (! isalnum (ch) && ch != '_') {
336 option_error("IPX router name must be alphanumeric or _");
337 return 0;
340 if (count >= sizeof (ipxcp_wantoptions[0].name) - 1) {
341 option_error("IPX router name is limited to %d characters",
342 sizeof (ipxcp_wantoptions[0].name) - 1);
343 return 0;
346 dest[count++] = toupper (ch);
348 dest[count] = 0;
350 return 1;
354 * ipxcp_init - Initialize IPXCP.
356 static void
357 ipxcp_init(unit)
358 int unit;
360 fsm *f = &ipxcp_fsm[unit];
362 f->unit = unit;
363 f->protocol = PPP_IPXCP;
364 f->callbacks = &ipxcp_callbacks;
365 fsm_init(&ipxcp_fsm[unit]);
367 memset (wo->name, 0, sizeof (wo->name));
368 memset (wo->our_node, 0, sizeof (wo->our_node));
369 memset (wo->his_node, 0, sizeof (wo->his_node));
371 wo->neg_nn = 1;
372 wo->neg_complete = 1;
373 wo->network = 0;
375 ao->neg_node = 1;
376 ao->neg_nn = 1;
377 ao->neg_name = 1;
378 ao->neg_complete = 1;
379 ao->neg_router = 1;
381 ao->accept_local = 0;
382 ao->accept_remote = 0;
383 ao->accept_network = 0;
385 wo->tried_rip = 0;
386 wo->tried_nlsp = 0;
390 * Copy the node number
393 static void
394 copy_node (src, dst)
395 u_char *src, *dst;
397 memcpy (dst, src, sizeof (ipxcp_wantoptions[0].our_node));
401 * Compare node numbers
404 static int
405 compare_node (src, dst)
406 u_char *src, *dst;
408 return memcmp (dst, src, sizeof (ipxcp_wantoptions[0].our_node)) == 0;
412 * Is the node number zero?
415 static int
416 zero_node (node)
417 u_char *node;
419 int indx;
420 for (indx = 0; indx < sizeof (ipxcp_wantoptions[0].our_node); ++indx)
421 if (node [indx] != 0)
422 return 0;
423 return 1;
427 * Increment the node number
430 static void
431 inc_node (node)
432 u_char *node;
434 u_char *outp;
435 u_int32_t magic_num;
437 outp = node;
438 magic_num = magic();
439 *outp++ = '\0';
440 *outp++ = '\0';
441 PUTLONG (magic_num, outp);
445 * ipxcp_open - IPXCP is allowed to come up.
447 static void
448 ipxcp_open(unit)
449 int unit;
451 fsm_open(&ipxcp_fsm[unit]);
455 * ipxcp_close - Take IPXCP down.
457 static void
458 ipxcp_close(unit, reason)
459 int unit;
460 char *reason;
462 fsm_close(&ipxcp_fsm[unit], reason);
467 * ipxcp_lowerup - The lower layer is up.
469 static void
470 ipxcp_lowerup(unit)
471 int unit;
473 fsm_lowerup(&ipxcp_fsm[unit]);
478 * ipxcp_lowerdown - The lower layer is down.
480 static void
481 ipxcp_lowerdown(unit)
482 int unit;
484 fsm_lowerdown(&ipxcp_fsm[unit]);
489 * ipxcp_input - Input IPXCP packet.
491 static void
492 ipxcp_input(unit, p, len)
493 int unit;
494 u_char *p;
495 int len;
497 fsm_input(&ipxcp_fsm[unit], p, len);
502 * ipxcp_protrej - A Protocol-Reject was received for IPXCP.
504 * Pretend the lower layer went down, so we shut up.
506 static void
507 ipxcp_protrej(unit)
508 int unit;
510 fsm_lowerdown(&ipxcp_fsm[unit]);
515 * ipxcp_resetci - Reset our CI.
517 static void
518 ipxcp_resetci(f)
519 fsm *f;
521 wo->req_node = wo->neg_node && ao->neg_node;
522 wo->req_nn = wo->neg_nn && ao->neg_nn;
524 if (wo->our_network == 0) {
525 wo->neg_node = 1;
526 ao->accept_network = 1;
529 * If our node number is zero then change it.
531 if (zero_node (wo->our_node)) {
532 inc_node (wo->our_node);
533 ao->accept_local = 1;
534 wo->neg_node = 1;
537 * If his node number is zero then change it.
539 if (zero_node (wo->his_node)) {
540 inc_node (wo->his_node);
541 ao->accept_remote = 1;
544 * If no routing agent was specified then we do RIP/SAP according to the
545 * RFC documents. If you have specified something then OK. Otherwise, we
546 * do RIP/SAP.
548 if (ao->router == 0) {
549 ao->router |= BIT(RIP_SAP);
550 wo->router |= BIT(RIP_SAP);
553 /* Always specify a routing protocol unless it was REJected. */
554 wo->neg_router = 1;
556 * Start with these default values
558 *go = *wo;
562 * ipxcp_cilen - Return length of our CI.
565 static int
566 ipxcp_cilen(f)
567 fsm *f;
569 int len;
571 len = go->neg_nn ? CILEN_NETN : 0;
572 len += go->neg_node ? CILEN_NODEN : 0;
573 len += go->neg_name ? CILEN_NAME + strlen (go->name) - 1 : 0;
575 /* RFC says that defaults should not be included. */
576 if (go->neg_router && to_external(go->router) != RIP_SAP)
577 len += CILEN_PROTOCOL;
579 return (len);
584 * ipxcp_addci - Add our desired CIs to a packet.
586 static void
587 ipxcp_addci(f, ucp, lenp)
588 fsm *f;
589 u_char *ucp;
590 int *lenp;
593 * Add the options to the record.
595 if (go->neg_nn) {
596 PUTCHAR (IPX_NETWORK_NUMBER, ucp);
597 PUTCHAR (CILEN_NETN, ucp);
598 PUTLONG (go->our_network, ucp);
601 if (go->neg_node) {
602 int indx;
603 PUTCHAR (IPX_NODE_NUMBER, ucp);
604 PUTCHAR (CILEN_NODEN, ucp);
605 for (indx = 0; indx < sizeof (go->our_node); ++indx)
606 PUTCHAR (go->our_node[indx], ucp);
609 if (go->neg_name) {
610 int cilen = strlen (go->name);
611 int indx;
612 PUTCHAR (IPX_ROUTER_NAME, ucp);
613 PUTCHAR (CILEN_NAME + cilen - 1, ucp);
614 for (indx = 0; indx < cilen; ++indx)
615 PUTCHAR (go->name [indx], ucp);
618 if (go->neg_router) {
619 short external = to_external (go->router);
620 if (external != RIP_SAP) {
621 PUTCHAR (IPX_ROUTER_PROTOCOL, ucp);
622 PUTCHAR (CILEN_PROTOCOL, ucp);
623 PUTSHORT (external, ucp);
629 * ipxcp_ackci - Ack our CIs.
631 * Returns:
632 * 0 - Ack was bad.
633 * 1 - Ack was good.
635 static int
636 ipxcp_ackci(f, p, len)
637 fsm *f;
638 u_char *p;
639 int len;
641 u_short cilen, citype, cishort;
642 u_char cichar;
643 u_int32_t cilong;
645 #define ACKCIVOID(opt, neg) \
646 if (neg) { \
647 if ((len -= CILEN_VOID) < 0) \
648 break; \
649 GETCHAR(citype, p); \
650 GETCHAR(cilen, p); \
651 if (cilen != CILEN_VOID || \
652 citype != opt) \
653 break; \
656 #define ACKCICOMPLETE(opt,neg) ACKCIVOID(opt, neg)
658 #define ACKCICHARS(opt, neg, val, cnt) \
659 if (neg) { \
660 int indx, count = cnt; \
661 len -= (count + 2); \
662 if (len < 0) \
663 break; \
664 GETCHAR(citype, p); \
665 GETCHAR(cilen, p); \
666 if (cilen != (count + 2) || \
667 citype != opt) \
668 break; \
669 for (indx = 0; indx < count; ++indx) {\
670 GETCHAR(cichar, p); \
671 if (cichar != ((u_char *) &val)[indx]) \
672 break; \
674 if (indx != count) \
675 break; \
678 #define ACKCINODE(opt,neg,val) ACKCICHARS(opt,neg,val,sizeof(val))
679 #define ACKCINAME(opt,neg,val) ACKCICHARS(opt,neg,val,strlen(val))
681 #define ACKCINETWORK(opt, neg, val) \
682 if (neg) { \
683 if ((len -= CILEN_NETN) < 0) \
684 break; \
685 GETCHAR(citype, p); \
686 GETCHAR(cilen, p); \
687 if (cilen != CILEN_NETN || \
688 citype != opt) \
689 break; \
690 GETLONG(cilong, p); \
691 if (cilong != val) \
692 break; \
695 #define ACKCIPROTO(opt, neg, val) \
696 if (neg) { \
697 if (len < 2) \
698 break; \
699 GETCHAR(citype, p); \
700 GETCHAR(cilen, p); \
701 if (cilen != CILEN_PROTOCOL || citype != opt) \
702 break; \
703 len -= cilen; \
704 if (len < 0) \
705 break; \
706 GETSHORT(cishort, p); \
707 if (cishort != to_external (val) || cishort == RIP_SAP) \
708 break; \
711 * Process the ACK frame in the order in which the frame was assembled
713 do {
714 ACKCINETWORK (IPX_NETWORK_NUMBER, go->neg_nn, go->our_network);
715 ACKCINODE (IPX_NODE_NUMBER, go->neg_node, go->our_node);
716 ACKCINAME (IPX_ROUTER_NAME, go->neg_name, go->name);
717 if (len > 0)
718 ACKCIPROTO (IPX_ROUTER_PROTOCOL, go->neg_router, go->router);
720 * This is the end of the record.
722 if (len == 0)
723 return (1);
724 } while (0);
726 * The frame is invalid
728 IPXCPDEBUG(("ipxcp_ackci: received bad Ack!"));
729 return (0);
733 * ipxcp_nakci - Peer has sent a NAK for some of our CIs.
734 * This should not modify any state if the Nak is bad
735 * or if IPXCP is in the OPENED state.
737 * Returns:
738 * 0 - Nak was bad.
739 * 1 - Nak was good.
742 static int
743 ipxcp_nakci(f, p, len)
744 fsm *f;
745 u_char *p;
746 int len;
748 u_char citype, cilen, *next;
749 u_short s;
750 u_int32_t l;
751 ipxcp_options no; /* options we've seen Naks for */
752 ipxcp_options try; /* options to request next time */
754 BZERO(&no, sizeof(no));
755 try = *go;
757 while (len > CILEN_VOID) {
758 GETCHAR (citype, p);
759 GETCHAR (cilen, p);
760 len -= cilen;
761 if (len < 0)
762 goto bad;
763 next = &p [cilen - CILEN_VOID];
765 switch (citype) {
766 case IPX_NETWORK_NUMBER:
767 if (!go->neg_nn || no.neg_nn || (cilen != CILEN_NETN))
768 goto bad;
769 no.neg_nn = 1;
771 GETLONG(l, p);
772 if (l && ao->accept_network)
773 try.our_network = l;
774 break;
776 case IPX_NODE_NUMBER:
777 if (!go->neg_node || no.neg_node || (cilen != CILEN_NODEN))
778 goto bad;
779 no.neg_node = 1;
781 if (!zero_node (p) && ao->accept_local &&
782 ! compare_node (p, ho->his_node))
783 copy_node (p, try.our_node);
784 break;
786 /* This has never been sent. Ignore the NAK frame */
787 case IPX_COMPRESSION_PROTOCOL:
788 goto bad;
790 case IPX_ROUTER_PROTOCOL:
791 if (!go->neg_router || (cilen < CILEN_PROTOCOL))
792 goto bad;
794 GETSHORT (s, p);
795 if (s > 15) /* This is just bad, but ignore for now. */
796 break;
798 s = BIT(s);
799 if (no.router & s) /* duplicate NAKs are always bad */
800 goto bad;
802 if (no.router == 0) /* Reset on first NAK only */
803 try.router = 0;
805 no.router |= s;
806 try.router |= s;
807 try.neg_router = 1;
808 break;
810 /* These, according to the RFC, must never be NAKed. */
811 case IPX_ROUTER_NAME:
812 case IPX_COMPLETE:
813 goto bad;
815 /* These are for options which we have not seen. */
816 default:
817 break;
819 p = next;
823 * Do not permit the peer to force a router protocol which we do not
824 * support. However, default to the condition that will accept "NONE".
826 try.router &= (ao->router | BIT(IPX_NONE));
827 if (try.router == 0 && ao->router != 0)
828 try.router = BIT(IPX_NONE);
830 if (try.router != 0)
831 try.neg_router = 1;
834 * OK, the Nak is good. Now we can update state.
835 * If there are any options left, we ignore them.
837 if (f->state != OPENED)
838 *go = try;
840 return 1;
842 bad:
843 IPXCPDEBUG(("ipxcp_nakci: received bad Nak!"));
844 return 0;
848 * ipxcp_rejci - Reject some of our CIs.
850 static int
851 ipxcp_rejci(f, p, len)
852 fsm *f;
853 u_char *p;
854 int len;
856 u_short cilen, citype, cishort;
857 u_char cichar;
858 u_int32_t cilong;
859 ipxcp_options try; /* options to request next time */
861 #define REJCINETWORK(opt, neg, val) \
862 if (neg && p[0] == opt) { \
863 if ((len -= CILEN_NETN) < 0) \
864 break; \
865 GETCHAR(citype, p); \
866 GETCHAR(cilen, p); \
867 if (cilen != CILEN_NETN || \
868 citype != opt) \
869 break; \
870 GETLONG(cilong, p); \
871 if (cilong != val) \
872 break; \
873 neg = 0; \
876 #define REJCICHARS(opt, neg, val, cnt) \
877 if (neg && p[0] == opt) { \
878 int indx, count = cnt; \
879 len -= (count + 2); \
880 if (len < 0) \
881 break; \
882 GETCHAR(citype, p); \
883 GETCHAR(cilen, p); \
884 if (cilen != (count + 2) || \
885 citype != opt) \
886 break; \
887 for (indx = 0; indx < count; ++indx) {\
888 GETCHAR(cichar, p); \
889 if (cichar != ((u_char *) &val)[indx]) \
890 break; \
892 if (indx != count) \
893 break; \
894 neg = 0; \
897 #define REJCINODE(opt,neg,val) REJCICHARS(opt,neg,val,sizeof(val))
898 #define REJCINAME(opt,neg,val) REJCICHARS(opt,neg,val,strlen(val))
900 #define REJCIVOID(opt, neg) \
901 if (neg && p[0] == opt) { \
902 if ((len -= CILEN_VOID) < 0) \
903 break; \
904 GETCHAR(citype, p); \
905 GETCHAR(cilen, p); \
906 if (cilen != CILEN_VOID || citype != opt) \
907 break; \
908 neg = 0; \
911 /* a reject for RIP/SAP is invalid since we don't send it and you can't
912 reject something which is not sent. (You can NAK, but you can't REJ.) */
913 #define REJCIPROTO(opt, neg, val, bit) \
914 if (neg && p[0] == opt) { \
915 if ((len -= CILEN_PROTOCOL) < 0) \
916 break; \
917 GETCHAR(citype, p); \
918 GETCHAR(cilen, p); \
919 if (cilen != CILEN_PROTOCOL) \
920 break; \
921 GETSHORT(cishort, p); \
922 if (cishort != to_external (val) || cishort == RIP_SAP) \
923 break; \
924 neg = 0; \
927 * Any Rejected 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 try = *go;
933 do {
934 REJCINETWORK (IPX_NETWORK_NUMBER, try.neg_nn, try.our_network);
935 REJCINODE (IPX_NODE_NUMBER, try.neg_node, try.our_node);
936 REJCINAME (IPX_ROUTER_NAME, try.neg_name, try.name);
937 REJCIPROTO (IPX_ROUTER_PROTOCOL, try.neg_router, try.router, 0);
939 * This is the end of the record.
941 if (len == 0) {
942 if (f->state != OPENED)
943 *go = try;
944 return (1);
946 } while (0);
948 * The frame is invalid at this point.
950 IPXCPDEBUG(("ipxcp_rejci: received bad Reject!"));
951 return 0;
955 * ipxcp_reqci - Check the peer's requested CIs and send appropriate response.
957 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
958 * appropriately. If reject_if_disagree is non-zero, doesn't return
959 * CONFNAK; returns CONFREJ if it can't return CONFACK.
961 static int
962 ipxcp_reqci(f, inp, len, reject_if_disagree)
963 fsm *f;
964 u_char *inp; /* Requested CIs */
965 int *len; /* Length of requested CIs */
966 int reject_if_disagree;
968 u_char *cip, *next; /* Pointer to current and next CIs */
969 u_short cilen, citype; /* Parsed len, type */
970 u_short cishort; /* Parsed short value */
971 u_int32_t cinetwork; /* Parsed address values */
972 int rc = CONFACK; /* Final packet return code */
973 int orc; /* Individual option return code */
974 u_char *p; /* Pointer to next char to parse */
975 u_char *ucp = inp; /* Pointer to current output char */
976 int l = *len; /* Length left */
979 * Reset all his options.
981 BZERO(ho, sizeof(*ho));
984 * Process all his options.
986 next = inp;
987 while (l) {
988 orc = CONFACK; /* Assume success */
989 cip = p = next; /* Remember begining of CI */
990 if (l < 2 || /* Not enough data for CI header or */
991 p[1] < 2 || /* CI length too small or */
992 p[1] > l) { /* CI length too big? */
993 IPXCPDEBUG(("ipxcp_reqci: bad CI length!"));
994 orc = CONFREJ; /* Reject bad CI */
995 cilen = l; /* Reject till end of packet */
996 l = 0; /* Don't loop again */
997 goto endswitch;
999 GETCHAR(citype, p); /* Parse CI type */
1000 GETCHAR(cilen, p); /* Parse CI length */
1001 l -= cilen; /* Adjust remaining length */
1002 next += cilen; /* Step to next CI */
1004 switch (citype) { /* Check CI type */
1006 * The network number must match. Choose the larger of the two.
1008 case IPX_NETWORK_NUMBER:
1009 /* if we wont negotiate the network number or the length is wrong
1010 then reject the option */
1011 if ( !ao->neg_nn || cilen != CILEN_NETN ) {
1012 orc = CONFREJ;
1013 break;
1015 GETLONG(cinetwork, p);
1017 /* If the network numbers match then acknowledge them. */
1018 if (cinetwork != 0) {
1019 ho->his_network = cinetwork;
1020 ho->neg_nn = 1;
1021 if (wo->our_network == cinetwork)
1022 break;
1024 * If the network number is not given or we don't accept their change or
1025 * the network number is too small then NAK it.
1027 if (! ao->accept_network || cinetwork < wo->our_network) {
1028 DECPTR (sizeof (u_int32_t), p);
1029 PUTLONG (wo->our_network, p);
1030 orc = CONFNAK;
1032 break;
1035 * The peer sent '0' for the network. Give it ours if we have one.
1037 if (go->our_network != 0) {
1038 DECPTR (sizeof (u_int32_t), p);
1039 PUTLONG (wo->our_network, p);
1040 orc = CONFNAK;
1042 * We don't have one. Reject the value.
1044 } else
1045 orc = CONFREJ;
1047 break;
1049 * The node number is required
1051 case IPX_NODE_NUMBER:
1052 /* if we wont negotiate the node number or the length is wrong
1053 then reject the option */
1054 if ( cilen != CILEN_NODEN ) {
1055 orc = CONFREJ;
1056 break;
1059 copy_node (p, ho->his_node);
1060 ho->neg_node = 1;
1062 * If the remote does not have a number and we do then NAK it with the value
1063 * which we have for it. (We never have a default value of zero.)
1065 if (zero_node (ho->his_node)) {
1066 orc = CONFNAK;
1067 copy_node (wo->his_node, p);
1068 INCPTR (sizeof (wo->his_node), p);
1069 break;
1072 * If you have given me the expected network node number then I'll accept
1073 * it now.
1075 if (compare_node (wo->his_node, ho->his_node)) {
1076 orc = CONFACK;
1077 ho->neg_node = 1;
1078 INCPTR (sizeof (wo->his_node), p);
1079 break;
1082 * If his node number is the same as ours then ask him to try the next
1083 * value.
1085 if (compare_node (ho->his_node, go->our_node)) {
1086 inc_node (ho->his_node);
1087 orc = CONFNAK;
1088 copy_node (ho->his_node, p);
1089 INCPTR (sizeof (wo->his_node), p);
1090 break;
1093 * If we don't accept a new value then NAK it.
1095 if (! ao->accept_remote) {
1096 copy_node (wo->his_node, p);
1097 INCPTR (sizeof (wo->his_node), p);
1098 orc = CONFNAK;
1099 break;
1101 orc = CONFACK;
1102 ho->neg_node = 1;
1103 INCPTR (sizeof (wo->his_node), p);
1104 break;
1106 * Compression is not desired at this time. It is always rejected.
1108 case IPX_COMPRESSION_PROTOCOL:
1109 orc = CONFREJ;
1110 break;
1112 * The routing protocol is a bitmask of various types. Any combination
1113 * of the values RIP_SAP and NLSP are permissible. 'IPX_NONE' for no
1114 * routing protocol must be specified only once.
1116 case IPX_ROUTER_PROTOCOL:
1117 if ( !ao->neg_router || cilen < CILEN_PROTOCOL ) {
1118 orc = CONFREJ;
1119 break;
1122 GETSHORT (cishort, p);
1124 if (wo->neg_router == 0) {
1125 wo->neg_router = 1;
1126 wo->router = BIT(IPX_NONE);
1129 if ((cishort == IPX_NONE && ho->router != 0) ||
1130 (ho->router & BIT(IPX_NONE))) {
1131 orc = CONFREJ;
1132 break;
1135 cishort = BIT(cishort);
1136 if (ho->router & cishort) {
1137 orc = CONFREJ;
1138 break;
1141 ho->router |= cishort;
1142 ho->neg_router = 1;
1144 /* Finally do not allow a router protocol which we do not
1145 support. */
1147 if ((cishort & (ao->router | BIT(IPX_NONE))) == 0) {
1148 int protocol;
1150 if (cishort == BIT(NLSP) &&
1151 (ao->router & BIT(RIP_SAP)) &&
1152 !wo->tried_rip) {
1153 protocol = RIP_SAP;
1154 wo->tried_rip = 1;
1155 } else
1156 protocol = IPX_NONE;
1158 DECPTR (sizeof (u_int16_t), p);
1159 PUTSHORT (protocol, p);
1160 orc = CONFNAK;
1162 break;
1164 * The router name is advisorary. Just accept it if it is not too large.
1166 case IPX_ROUTER_NAME:
1167 if (cilen >= CILEN_NAME) {
1168 int name_size = cilen - CILEN_NAME;
1169 if (name_size > sizeof (ho->name))
1170 name_size = sizeof (ho->name) - 1;
1171 memset (ho->name, 0, sizeof (ho->name));
1172 memcpy (ho->name, p, name_size);
1173 ho->name [name_size] = '\0';
1174 ho->neg_name = 1;
1175 orc = CONFACK;
1176 break;
1178 orc = CONFREJ;
1179 break;
1181 * This is advisorary.
1183 case IPX_COMPLETE:
1184 if (cilen != CILEN_COMPLETE)
1185 orc = CONFREJ;
1186 else {
1187 ho->neg_complete = 1;
1188 orc = CONFACK;
1190 break;
1192 * All other entries are not known at this time.
1194 default:
1195 orc = CONFREJ;
1196 break;
1198 endswitch:
1199 if (orc == CONFACK && /* Good CI */
1200 rc != CONFACK) /* but prior CI wasnt? */
1201 continue; /* Don't send this one */
1203 if (orc == CONFNAK) { /* Nak this CI? */
1204 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1205 orc = CONFREJ; /* Get tough if so */
1206 if (rc == CONFREJ) /* Rejecting prior CI? */
1207 continue; /* Don't send this one */
1208 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1209 rc = CONFNAK; /* Not anymore... */
1210 ucp = inp; /* Backup */
1214 if (orc == CONFREJ && /* Reject this CI */
1215 rc != CONFREJ) { /* but no prior ones? */
1216 rc = CONFREJ;
1217 ucp = inp; /* Backup */
1220 /* Need to move CI? */
1221 if (ucp != cip)
1222 BCOPY(cip, ucp, cilen); /* Move it */
1224 /* Update output pointer */
1225 INCPTR(cilen, ucp);
1229 * If we aren't rejecting this packet, and we want to negotiate
1230 * their address, and they didn't send their address, then we
1231 * send a NAK with a IPX_NODE_NUMBER option appended. We assume the
1232 * input buffer is long enough that we can append the extra
1233 * option safely.
1236 if (rc != CONFREJ && !ho->neg_node &&
1237 wo->req_nn && !reject_if_disagree) {
1238 if (rc == CONFACK) {
1239 rc = CONFNAK;
1240 wo->req_nn = 0; /* don't ask again */
1241 ucp = inp; /* reset pointer */
1244 if (zero_node (wo->his_node))
1245 inc_node (wo->his_node);
1247 PUTCHAR (IPX_NODE_NUMBER, ucp);
1248 PUTCHAR (CILEN_NODEN, ucp);
1249 copy_node (wo->his_node, ucp);
1250 INCPTR (sizeof (wo->his_node), ucp);
1253 *len = ucp - inp; /* Compute output length */
1254 IPXCPDEBUG(("ipxcp: returning Configure-%s", CODENAME(rc)));
1255 return (rc); /* Return final code */
1259 * ipxcp_up - IPXCP has come UP.
1261 * Configure the IP network interface appropriately and bring it up.
1264 static void
1265 ipxcp_up(f)
1266 fsm *f;
1268 int unit = f->unit;
1270 IPXCPDEBUG(("ipxcp: up"));
1272 /* The default router protocol is RIP/SAP. */
1273 if (ho->router == 0)
1274 ho->router = BIT(RIP_SAP);
1276 if (go->router == 0)
1277 go->router = BIT(RIP_SAP);
1279 /* Fetch the network number */
1280 if (!ho->neg_nn)
1281 ho->his_network = wo->his_network;
1283 if (!ho->neg_node)
1284 copy_node (wo->his_node, ho->his_node);
1286 if (!wo->neg_node && !go->neg_node)
1287 copy_node (wo->our_node, go->our_node);
1289 if (zero_node (go->our_node)) {
1290 static char errmsg[] = "Could not determine local IPX node address";
1291 if (debug)
1292 error(errmsg);
1293 ipxcp_close(f->unit, errmsg);
1294 return;
1297 go->network = go->our_network;
1298 if (ho->his_network != 0 && ho->his_network > go->network)
1299 go->network = ho->his_network;
1301 if (go->network == 0) {
1302 static char errmsg[] = "Can not determine network number";
1303 if (debug)
1304 error(errmsg);
1305 ipxcp_close (unit, errmsg);
1306 return;
1309 /* bring the interface up */
1310 if (!sifup(unit)) {
1311 if (debug)
1312 warn("sifup failed (IPX)");
1313 ipxcp_close(unit, "Interface configuration failed");
1314 return;
1316 ipxcp_is_up = 1;
1318 /* set the network number for IPX */
1319 if (!sipxfaddr(unit, go->network, go->our_node)) {
1320 if (debug)
1321 warn("sipxfaddr failed");
1322 ipxcp_close(unit, "Interface configuration failed");
1323 return;
1326 np_up(f->unit, PPP_IPX);
1329 * Execute the ipx-up script, like this:
1330 * /etc/ppp/ipx-up interface tty speed local-IPX remote-IPX
1333 ipxcp_script (f, _PATH_IPXUP);
1337 * ipxcp_down - IPXCP has gone DOWN.
1339 * Take the IP network interface down, clear its addresses
1340 * and delete routes through it.
1343 static void
1344 ipxcp_down(f)
1345 fsm *f;
1347 IPXCPDEBUG(("ipxcp: down"));
1349 if (!ipxcp_is_up)
1350 return;
1351 ipxcp_is_up = 0;
1352 np_down(f->unit, PPP_IPX);
1353 cipxfaddr(f->unit);
1354 sifnpmode(f->unit, PPP_IPX, NPMODE_DROP);
1355 sifdown(f->unit);
1356 ipxcp_script (f, _PATH_IPXDOWN);
1361 * ipxcp_finished - possibly shut down the lower layers.
1363 static void
1364 ipxcp_finished(f)
1365 fsm *f;
1367 np_finished(f->unit, PPP_IPX);
1372 * ipxcp_script - Execute a script with arguments
1373 * interface-name tty-name speed local-IPX remote-IPX networks.
1375 static void
1376 ipxcp_script(f, script)
1377 fsm *f;
1378 char *script;
1380 char strspeed[32], strlocal[32], strremote[32];
1381 char strnetwork[32], strpid[32];
1382 char *argv[14], strproto_lcl[32], strproto_rmt[32];
1384 slprintf(strpid, sizeof(strpid), "%d", getpid());
1385 slprintf(strspeed, sizeof(strspeed),"%d", baud_rate);
1387 strproto_lcl[0] = '\0';
1388 if (go->neg_router && ((go->router & BIT(IPX_NONE)) == 0)) {
1389 if (go->router & BIT(RIP_SAP))
1390 strlcpy (strproto_lcl, "RIP ", sizeof(strproto_lcl));
1391 if (go->router & BIT(NLSP))
1392 strlcat (strproto_lcl, "NLSP ", sizeof(strproto_lcl));
1395 if (strproto_lcl[0] == '\0')
1396 strlcpy (strproto_lcl, "NONE ", sizeof(strproto_lcl));
1398 strproto_lcl[strlen (strproto_lcl)-1] = '\0';
1400 strproto_rmt[0] = '\0';
1401 if (ho->neg_router && ((ho->router & BIT(IPX_NONE)) == 0)) {
1402 if (ho->router & BIT(RIP_SAP))
1403 strlcpy (strproto_rmt, "RIP ", sizeof(strproto_rmt));
1404 if (ho->router & BIT(NLSP))
1405 strlcat (strproto_rmt, "NLSP ", sizeof(strproto_rmt));
1408 if (strproto_rmt[0] == '\0')
1409 strlcpy (strproto_rmt, "NONE ", sizeof(strproto_rmt));
1411 strproto_rmt[strlen (strproto_rmt)-1] = '\0';
1413 strlcpy (strnetwork, ipx_ntoa (go->network), sizeof(strnetwork));
1415 slprintf (strlocal, sizeof(strlocal), "%0.6B", go->our_node);
1417 slprintf (strremote, sizeof(strremote), "%0.6B", ho->his_node);
1419 argv[0] = script;
1420 argv[1] = ifname;
1421 argv[2] = devnam;
1422 argv[3] = strspeed;
1423 argv[4] = strnetwork;
1424 argv[5] = strlocal;
1425 argv[6] = strremote;
1426 argv[7] = strproto_lcl;
1427 argv[8] = strproto_rmt;
1428 argv[9] = go->name;
1429 argv[10] = ho->name;
1430 argv[11] = ipparam;
1431 argv[12] = strpid;
1432 argv[13] = NULL;
1433 run_program(script, argv, 0, NULL, NULL);
1437 * ipxcp_printpkt - print the contents of an IPXCP packet.
1439 static char *ipxcp_codenames[] = {
1440 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1441 "TermReq", "TermAck", "CodeRej"
1444 static int
1445 ipxcp_printpkt(p, plen, printer, arg)
1446 u_char *p;
1447 int plen;
1448 void (*printer) __P((void *, char *, ...));
1449 void *arg;
1451 int code, id, len, olen;
1452 u_char *pstart, *optend;
1453 u_short cishort;
1454 u_int32_t cilong;
1456 if (plen < HEADERLEN)
1457 return 0;
1458 pstart = p;
1459 GETCHAR(code, p);
1460 GETCHAR(id, p);
1461 GETSHORT(len, p);
1462 if (len < HEADERLEN || len > plen)
1463 return 0;
1465 if (code >= 1 && code <= sizeof(ipxcp_codenames) / sizeof(char *))
1466 printer(arg, " %s", ipxcp_codenames[code-1]);
1467 else
1468 printer(arg, " code=0x%x", code);
1469 printer(arg, " id=0x%x", id);
1470 len -= HEADERLEN;
1471 switch (code) {
1472 case CONFREQ:
1473 case CONFACK:
1474 case CONFNAK:
1475 case CONFREJ:
1476 /* print option list */
1477 while (len >= 2) {
1478 GETCHAR(code, p);
1479 GETCHAR(olen, p);
1480 p -= 2;
1481 if (olen < CILEN_VOID || olen > len) {
1482 break;
1484 printer(arg, " <");
1485 len -= olen;
1486 optend = p + olen;
1487 switch (code) {
1488 case IPX_NETWORK_NUMBER:
1489 if (olen == CILEN_NETN) {
1490 p += 2;
1491 GETLONG(cilong, p);
1492 printer (arg, "network %s", ipx_ntoa (cilong));
1494 break;
1495 case IPX_NODE_NUMBER:
1496 if (olen == CILEN_NODEN) {
1497 p += 2;
1498 printer (arg, "node ");
1499 while (p < optend) {
1500 GETCHAR(code, p);
1501 printer(arg, "%.2x", (int) (unsigned int) (unsigned char) code);
1504 break;
1505 case IPX_COMPRESSION_PROTOCOL:
1506 if (olen == CILEN_COMPRESS) {
1507 p += 2;
1508 GETSHORT (cishort, p);
1509 printer (arg, "compression %d", (int) cishort);
1511 break;
1512 case IPX_ROUTER_PROTOCOL:
1513 if (olen == CILEN_PROTOCOL) {
1514 p += 2;
1515 GETSHORT (cishort, p);
1516 printer (arg, "router proto %d", (int) cishort);
1518 break;
1519 case IPX_ROUTER_NAME:
1520 if (olen >= CILEN_NAME) {
1521 p += 2;
1522 printer (arg, "router name \"");
1523 while (p < optend) {
1524 GETCHAR(code, p);
1525 if (code >= 0x20 && code <= 0x7E)
1526 printer (arg, "%c", (int) (unsigned int) (unsigned char) code);
1527 else
1528 printer (arg, " \\%.2x", (int) (unsigned int) (unsigned char) code);
1530 printer (arg, "\"");
1532 break;
1533 case IPX_COMPLETE:
1534 if (olen == CILEN_COMPLETE) {
1535 p += 2;
1536 printer (arg, "complete");
1538 break;
1539 default:
1540 break;
1543 while (p < optend) {
1544 GETCHAR(code, p);
1545 printer(arg, " %.2x", (int) (unsigned int) (unsigned char) code);
1547 printer(arg, ">");
1549 break;
1551 case TERMACK:
1552 case TERMREQ:
1553 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1554 printer(arg, " ");
1555 print_string(p, len, printer, arg);
1556 p += len;
1557 len = 0;
1559 break;
1562 /* print the rest of the bytes in the packet */
1563 for (; len > 0; --len) {
1564 GETCHAR(code, p);
1565 printer(arg, " %.2x", (int) (unsigned int) (unsigned char) code);
1568 return p - pstart;
1570 #endif /* ifdef IPX_CHANGE */