1 /* NetBSD */
2
3 /*
4 * ipxcp.c - PPP IPX Control Protocol.
5 *
6 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The name "Carnegie Mellon University" must not be used to
21 * endorse or promote products derived from this software without
22 * prior written permission. For permission or any legal
23 * details, please contact
24 * Office of Technology Transfer
25 * Carnegie Mellon University
26 * 5000 Forbes Avenue
27 * Pittsburgh, PA 15213-3890
28 * (412) 268-4387, fax: (412) 268-7395
29 * tech-transfer@andrew.cmu.edu
30 *
31 * 4. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by Computing Services
34 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
35 *
36 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 */
44
45 #include <sys/cdefs.h>
46 #if 0
47 #define RCSID "Id: ipxcp.c,v 1.24 2005/08/25 23:59:34 paulus Exp "
48 static const char rcsid[] = RCSID;
49 #else
50 __RCSID("$NetBSD: ipxcp.c,v 1.5 2021/01/09 16:39:28 christos Exp $");
51 #endif
52 #ifdef IPX_CHANGE
53
54 /*
55 * TODO:
56 */
57
58 #include <stdio.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <ctype.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65
66 #include "pppd.h"
67 #include "fsm.h"
68 #include "ipxcp.h"
69 #include "pathnames.h"
70 #include "magic.h"
71
72
73 /* global vars */
74 ipxcp_options ipxcp_wantoptions[NUM_PPP]; /* Options that we want to request */
75 ipxcp_options ipxcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
76 ipxcp_options ipxcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
77 ipxcp_options ipxcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
78
79 #define wo (&ipxcp_wantoptions[0])
80 #define ao (&ipxcp_allowoptions[0])
81 #define go (&ipxcp_gotoptions[0])
82 #define ho (&ipxcp_hisoptions[0])
83
84 /*
85 * Callbacks for fsm code. (CI = Configuration Information)
86 */
87 static void ipxcp_resetci (fsm *); /* Reset our CI */
88 static int ipxcp_cilen (fsm *); /* Return length of our CI */
89 static void ipxcp_addci (fsm *, u_char *, int *); /* Add our CI */
90 static int ipxcp_ackci (fsm *, u_char *, int); /* Peer ack'd our CI */
91 static int ipxcp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
92 static int ipxcp_rejci (fsm *, u_char *, int); /* Peer rej'd our CI */
93 static int ipxcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
94 static void ipxcp_up (fsm *); /* We're UP */
95 static void ipxcp_down (fsm *); /* We're DOWN */
96 static void ipxcp_finished (fsm *); /* Don't need lower layer */
97 static void ipxcp_script (fsm *, char *); /* Run an up/down script */
98
99 fsm ipxcp_fsm[NUM_PPP]; /* IPXCP fsm structure */
100
101 static fsm_callbacks ipxcp_callbacks = { /* IPXCP callback routines */
102 ipxcp_resetci, /* Reset our Configuration Information */
103 ipxcp_cilen, /* Length of our Configuration Information */
104 ipxcp_addci, /* Add our Configuration Information */
105 ipxcp_ackci, /* ACK our Configuration Information */
106 ipxcp_nakci, /* NAK our Configuration Information */
107 ipxcp_rejci, /* Reject our Configuration Information */
108 ipxcp_reqci, /* Request peer's Configuration Information */
109 ipxcp_up, /* Called when fsm reaches OPENED state */
110 ipxcp_down, /* Called when fsm leaves OPENED state */
111 NULL, /* Called when we want the lower layer up */
112 ipxcp_finished, /* Called when we want the lower layer down */
113 NULL, /* Called when Protocol-Reject received */
114 NULL, /* Retransmission is necessary */
115 NULL, /* Called to handle protocol-specific codes */
116 "IPXCP" /* String name of protocol */
117 };
118
119 /*
120 * Command-line options.
121 */
122 static int setipxnode (char **);
123 static void printipxnode (option_t *,
124 void (*)(void *, char *, ...), void *);
125 static int setipxname (char **);
126
127 static option_t ipxcp_option_list[] = {
128 { "ipx", o_bool, &ipxcp_protent.enabled_flag,
129 "Enable IPXCP (and IPX)", OPT_PRIO | 1 },
130 { "+ipx", o_bool, &ipxcp_protent.enabled_flag,
131 "Enable IPXCP (and IPX)", OPT_PRIOSUB | OPT_ALIAS | 1 },
132 { "noipx", o_bool, &ipxcp_protent.enabled_flag,
133 "Disable IPXCP (and IPX)", OPT_PRIOSUB },
134 { "-ipx", o_bool, &ipxcp_protent.enabled_flag,
135 "Disable IPXCP (and IPX)", OPT_PRIOSUB | OPT_ALIAS },
136
137 { "ipx-network", o_uint32, &ipxcp_wantoptions[0].our_network,
138 "Set our IPX network number", OPT_PRIO, &ipxcp_wantoptions[0].neg_nn },
139
140 { "ipxcp-accept-network", o_bool, &ipxcp_wantoptions[0].accept_network,
141 "Accept peer IPX network number", 1,
142 &ipxcp_allowoptions[0].accept_network },
143
144 { "ipx-node", o_special, (void *)setipxnode,
145 "Set IPX node number", OPT_A2PRINTER, (void *)printipxnode },
146
147 { "ipxcp-accept-local", o_bool, &ipxcp_wantoptions[0].accept_local,
148 "Accept our IPX address", 1,
149 &ipxcp_allowoptions[0].accept_local },
150
151 { "ipxcp-accept-remote", o_bool, &ipxcp_wantoptions[0].accept_remote,
152 "Accept peer's IPX address", 1,
153 &ipxcp_allowoptions[0].accept_remote },
154
155 { "ipx-routing", o_int, &ipxcp_wantoptions[0].router,
156 "Set IPX routing proto number", OPT_PRIO,
157 &ipxcp_wantoptions[0].neg_router },
158
159 { "ipx-router-name", o_special, setipxname,
160 "Set IPX router name", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC,
161 &ipxcp_wantoptions[0].name },
162
163 { "ipxcp-restart", o_int, &ipxcp_fsm[0].timeouttime,
164 "Set timeout for IPXCP", OPT_PRIO },
165 { "ipxcp-max-terminate", o_int, &ipxcp_fsm[0].maxtermtransmits,
166 "Set max #xmits for IPXCP term-reqs", OPT_PRIO },
167 { "ipxcp-max-configure", o_int, &ipxcp_fsm[0].maxconfreqtransmits,
168 "Set max #xmits for IPXCP conf-reqs", OPT_PRIO },
169 { "ipxcp-max-failure", o_int, &ipxcp_fsm[0].maxnakloops,
170 "Set max #conf-naks for IPXCP", OPT_PRIO },
171
172 { NULL }
173 };
174
175 /*
176 * Protocol entry points.
177 */
178
179 static void ipxcp_init (int);
180 static void ipxcp_open (int);
181 static void ipxcp_close (int, char *);
182 static void ipxcp_lowerup (int);
183 static void ipxcp_lowerdown (int);
184 static void ipxcp_input (int, u_char *, int);
185 static void ipxcp_protrej (int);
186 static int ipxcp_printpkt (u_char *, int,
187 void (*) (void *, char *, ...), void *);
188
189 struct protent ipxcp_protent = {
190 PPP_IPXCP,
191 ipxcp_init,
192 ipxcp_input,
193 ipxcp_protrej,
194 ipxcp_lowerup,
195 ipxcp_lowerdown,
196 ipxcp_open,
197 ipxcp_close,
198 ipxcp_printpkt,
199 NULL,
200 0,
201 "IPXCP",
202 "IPX",
203 ipxcp_option_list,
204 NULL,
205 NULL,
206 NULL
207 };
208
209 /*
210 * Lengths of configuration options.
211 */
212
213 #define CILEN_VOID 2
214 #define CILEN_COMPLETE 2 /* length of complete option */
215 #define CILEN_NETN 6 /* network number length option */
216 #define CILEN_NODEN 8 /* node number length option */
217 #define CILEN_PROTOCOL 4 /* Minimum length of routing protocol */
218 #define CILEN_NAME 3 /* Minimum length of router name */
219 #define CILEN_COMPRESS 4 /* Minimum length of compression protocol */
220
221 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
222 (x) == CONFNAK ? "NAK" : "REJ")
223
224 static int ipxcp_is_up;
225
226 static char *ipx_ntoa (u_int32_t);
227
228 /* Used in printing the node number */
229 #define NODE(base) base[0], base[1], base[2], base[3], base[4], base[5]
230
231 /* Used to generate the proper bit mask */
232 #define BIT(num) (1 << (num))
233
234 /*
235 * Convert from internal to external notation
236 */
237
238 static short int
to_external(short int internal)239 to_external(short int internal)
240 {
241 short int external;
242
243 if (internal & BIT(IPX_NONE) )
244 external = IPX_NONE;
245 else
246 external = RIP_SAP;
247
248 return external;
249 }
250
251 /*
252 * Make a string representation of a network IP address.
253 */
254
255 static char *
ipx_ntoa(u_int32_t ipxaddr)256 ipx_ntoa(u_int32_t ipxaddr)
257 {
258 static char b[64];
259 slprintf(b, sizeof(b), "%x", ipxaddr);
260 return b;
261 }
262
263
264 static u_char *
setipxnodevalue(u_char * src,u_char * dst)265 setipxnodevalue(u_char *src, u_char *dst)
266 {
267 int indx;
268 int item;
269
270 for (;;) {
271 if (!isxdigit (*src))
272 break;
273
274 for (indx = 0; indx < 5; ++indx) {
275 dst[indx] <<= 4;
276 dst[indx] |= (dst[indx + 1] >> 4) & 0x0F;
277 }
278
279 item = toupper (*src) - '0';
280 if (item > 9)
281 item -= 7;
282
283 dst[5] = (dst[5] << 4) | item;
284 ++src;
285 }
286 return src;
287 }
288
289 static int ipx_prio_our, ipx_prio_his;
290
291 static int
setipxnode(char ** argv)292 setipxnode(char **argv)
293 {
294 u_char *end;
295 int have_his = 0;
296 u_char our_node[6];
297 u_char his_node[6];
298
299 memset (our_node, 0, 6);
300 memset (his_node, 0, 6);
301
302 end = setipxnodevalue ((u_char *)*argv, our_node);
303 if (*end == ':') {
304 have_his = 1;
305 end = setipxnodevalue (++end, his_node);
306 }
307
308 if (*end == '\0') {
309 ipxcp_wantoptions[0].neg_node = 1;
310 if (option_priority >= ipx_prio_our) {
311 memcpy(&ipxcp_wantoptions[0].our_node[0], our_node, 6);
312 ipx_prio_our = option_priority;
313 }
314 if (have_his && option_priority >= ipx_prio_his) {
315 memcpy(&ipxcp_wantoptions[0].his_node[0], his_node, 6);
316 ipx_prio_his = option_priority;
317 }
318 return 1;
319 }
320
321 option_error("invalid parameter '%s' for ipx-node option", *argv);
322 return 0;
323 }
324
325 static void
printipxnode(option_t * opt,void (* printer)(void *,char *,...),void * arg)326 printipxnode(option_t *opt, void (*printer) (void *, char *, ...), void *arg)
327 {
328 unsigned char *p;
329
330 p = ipxcp_wantoptions[0].our_node;
331 if (ipx_prio_our)
332 printer(arg, "%.2x%.2x%.2x%.2x%.2x%.2x",
333 p[0], p[1], p[2], p[3], p[4], p[5]);
334 printer(arg, ":");
335 p = ipxcp_wantoptions[0].his_node;
336 if (ipx_prio_his)
337 printer(arg, "%.2x%.2x%.2x%.2x%.2x%.2x",
338 p[0], p[1], p[2], p[3], p[4], p[5]);
339 }
340
341 static int
setipxname(char ** argv)342 setipxname (char **argv)
343 {
344 u_char *dest = ipxcp_wantoptions[0].name;
345 char *src = *argv;
346 int count;
347 char ch;
348
349 ipxcp_wantoptions[0].neg_name = 1;
350 ipxcp_allowoptions[0].neg_name = 1;
351 memset (dest, '\0', sizeof (ipxcp_wantoptions[0].name));
352
353 count = 0;
354 while (*src) {
355 ch = *src++;
356 if (! isalnum (ch) && ch != '_') {
357 option_error("IPX router name must be alphanumeric or _");
358 return 0;
359 }
360
361 if (count >= sizeof (ipxcp_wantoptions[0].name) - 1) {
362 option_error("IPX router name is limited to %d characters",
363 sizeof (ipxcp_wantoptions[0].name) - 1);
364 return 0;
365 }
366
367 dest[count++] = toupper (ch);
368 }
369 dest[count] = 0;
370
371 return 1;
372 }
373
374 /*
375 * ipxcp_init - Initialize IPXCP.
376 */
377 static void
ipxcp_init(int unit)378 ipxcp_init(int unit)
379 {
380 fsm *f = &ipxcp_fsm[unit];
381
382 f->unit = unit;
383 f->protocol = PPP_IPXCP;
384 f->callbacks = &ipxcp_callbacks;
385 fsm_init(&ipxcp_fsm[unit]);
386
387 memset (wo->name, 0, sizeof (wo->name));
388 memset (wo->our_node, 0, sizeof (wo->our_node));
389 memset (wo->his_node, 0, sizeof (wo->his_node));
390
391 wo->neg_nn = 1;
392 wo->neg_complete = 1;
393 wo->network = 0;
394
395 ao->neg_node = 1;
396 ao->neg_nn = 1;
397 ao->neg_name = 1;
398 ao->neg_complete = 1;
399 ao->neg_router = 1;
400
401 ao->accept_local = 0;
402 ao->accept_remote = 0;
403 ao->accept_network = 0;
404
405 wo->tried_rip = 0;
406 wo->tried_nlsp = 0;
407 }
408
409 /*
410 * Copy the node number
411 */
412
413 static void
copy_node(u_char * src,u_char * dst)414 copy_node (u_char *src, u_char *dst)
415 {
416 memcpy (dst, src, sizeof (ipxcp_wantoptions[0].our_node));
417 }
418
419 /*
420 * Compare node numbers
421 */
422
423 static int
compare_node(u_char * src,u_char * dst)424 compare_node (u_char *src, u_char *dst)
425 {
426 return memcmp (dst, src, sizeof (ipxcp_wantoptions[0].our_node)) == 0;
427 }
428
429 /*
430 * Is the node number zero?
431 */
432
433 static int
zero_node(u_char * node)434 zero_node (u_char *node)
435 {
436 int indx;
437 for (indx = 0; indx < sizeof (ipxcp_wantoptions[0].our_node); ++indx)
438 if (node [indx] != 0)
439 return 0;
440 return 1;
441 }
442
443 /*
444 * Increment the node number
445 */
446
447 static void
inc_node(u_char * node)448 inc_node (u_char *node)
449 {
450 u_char *outp;
451 u_int32_t magic_num;
452
453 outp = node;
454 magic_num = magic();
455 *outp++ = '\0';
456 *outp++ = '\0';
457 PUTLONG (magic_num, outp);
458 }
459
460 /*
461 * ipxcp_open - IPXCP is allowed to come up.
462 */
463 static void
ipxcp_open(int unit)464 ipxcp_open(int unit)
465 {
466 fsm_open(&ipxcp_fsm[unit]);
467 }
468
469 /*
470 * ipxcp_close - Take IPXCP down.
471 */
472 static void
ipxcp_close(int unit,char * reason)473 ipxcp_close(int unit, char *reason)
474 {
475 fsm_close(&ipxcp_fsm[unit], reason);
476 }
477
478
479 /*
480 * ipxcp_lowerup - The lower layer is up.
481 */
482 static void
ipxcp_lowerup(int unit)483 ipxcp_lowerup(int unit)
484 {
485 fsm_lowerup(&ipxcp_fsm[unit]);
486 }
487
488
489 /*
490 * ipxcp_lowerdown - The lower layer is down.
491 */
492 static void
ipxcp_lowerdown(int unit)493 ipxcp_lowerdown(int unit)
494 {
495 fsm_lowerdown(&ipxcp_fsm[unit]);
496 }
497
498
499 /*
500 * ipxcp_input - Input IPXCP packet.
501 */
502 static void
ipxcp_input(int unit,u_char * p,int len)503 ipxcp_input(int unit, u_char *p, int len)
504 {
505 fsm_input(&ipxcp_fsm[unit], p, len);
506 }
507
508
509 /*
510 * ipxcp_protrej - A Protocol-Reject was received for IPXCP.
511 *
512 * Pretend the lower layer went down, so we shut up.
513 */
514 static void
ipxcp_protrej(int unit)515 ipxcp_protrej(int unit)
516 {
517 fsm_lowerdown(&ipxcp_fsm[unit]);
518 }
519
520
521 /*
522 * ipxcp_resetci - Reset our CI.
523 */
524 static void
ipxcp_resetci(fsm * f)525 ipxcp_resetci(fsm *f)
526 {
527 wo->req_node = wo->neg_node && ao->neg_node;
528 wo->req_nn = wo->neg_nn && ao->neg_nn;
529
530 if (wo->our_network == 0) {
531 wo->neg_node = 1;
532 ao->accept_network = 1;
533 }
534 /*
535 * If our node number is zero then change it.
536 */
537 if (zero_node (wo->our_node)) {
538 inc_node (wo->our_node);
539 ao->accept_local = 1;
540 wo->neg_node = 1;
541 }
542 /*
543 * If his node number is zero then change it.
544 */
545 if (zero_node (wo->his_node)) {
546 inc_node (wo->his_node);
547 ao->accept_remote = 1;
548 }
549 /*
550 * If no routing agent was specified then we do RIP/SAP according to the
551 * RFC documents. If you have specified something then OK. Otherwise, we
552 * do RIP/SAP.
553 */
554 if (ao->router == 0) {
555 ao->router |= BIT(RIP_SAP);
556 wo->router |= BIT(RIP_SAP);
557 }
558
559 /* Always specify a routing protocol unless it was REJected. */
560 wo->neg_router = 1;
561 /*
562 * Start with these default values
563 */
564 *go = *wo;
565 }
566
567 /*
568 * ipxcp_cilen - Return length of our CI.
569 */
570
571 static int
ipxcp_cilen(fsm * f)572 ipxcp_cilen(fsm *f)
573 {
574 int len;
575
576 len = go->neg_nn ? CILEN_NETN : 0;
577 len += go->neg_node ? CILEN_NODEN : 0;
578 len += go->neg_name ? CILEN_NAME + strlen ((char *)go->name) - 1 : 0;
579
580 /* RFC says that defaults should not be included. */
581 if (go->neg_router && to_external(go->router) != RIP_SAP)
582 len += CILEN_PROTOCOL;
583
584 return (len);
585 }
586
587
588 /*
589 * ipxcp_addci - Add our desired CIs to a packet.
590 */
591 static void
ipxcp_addci(fsm * f,u_char * ucp,int * lenp)592 ipxcp_addci(fsm *f, u_char *ucp, int *lenp)
593 {
594 /*
595 * Add the options to the record.
596 */
597 if (go->neg_nn) {
598 PUTCHAR (IPX_NETWORK_NUMBER, ucp);
599 PUTCHAR (CILEN_NETN, ucp);
600 PUTLONG (go->our_network, ucp);
601 }
602
603 if (go->neg_node) {
604 int indx;
605 PUTCHAR (IPX_NODE_NUMBER, ucp);
606 PUTCHAR (CILEN_NODEN, ucp);
607 for (indx = 0; indx < sizeof (go->our_node); ++indx)
608 PUTCHAR (go->our_node[indx], ucp);
609 }
610
611 if (go->neg_name) {
612 int cilen = strlen ((char *)go->name);
613 int indx;
614 PUTCHAR (IPX_ROUTER_NAME, ucp);
615 PUTCHAR (CILEN_NAME + cilen - 1, ucp);
616 for (indx = 0; indx < cilen; ++indx)
617 PUTCHAR (go->name [indx], ucp);
618 }
619
620 if (go->neg_router) {
621 short external = to_external (go->router);
622 if (external != RIP_SAP) {
623 PUTCHAR (IPX_ROUTER_PROTOCOL, ucp);
624 PUTCHAR (CILEN_PROTOCOL, ucp);
625 PUTSHORT (external, ucp);
626 }
627 }
628 }
629
630 /*
631 * ipxcp_ackci - Ack our CIs.
632 *
633 * Returns:
634 * 0 - Ack was bad.
635 * 1 - Ack was good.
636 */
637 static int
ipxcp_ackci(fsm * f,u_char * p,int len)638 ipxcp_ackci(fsm *f, u_char *p, int len)
639 {
640 u_short cilen, citype, cishort;
641 u_char cichar;
642 u_int32_t cilong;
643
644 #define ACKCIVOID(opt, neg) \
645 if (neg) { \
646 if ((len -= CILEN_VOID) < 0) \
647 break; \
648 GETCHAR(citype, p); \
649 GETCHAR(cilen, p); \
650 if (cilen != CILEN_VOID || \
651 citype != opt) \
652 break; \
653 }
654
655 #define ACKCICOMPLETE(opt,neg) ACKCIVOID(opt, neg)
656
657 #define ACKCICHARS(opt, neg, val, cnt) \
658 if (neg) { \
659 int indx, count = cnt; \
660 len -= (count + 2); \
661 if (len < 0) \
662 break; \
663 GETCHAR(citype, p); \
664 GETCHAR(cilen, p); \
665 if (cilen != (count + 2) || \
666 citype != opt) \
667 break; \
668 for (indx = 0; indx < count; ++indx) {\
669 GETCHAR(cichar, p); \
670 if (cichar != ((u_char *) &val)[indx]) \
671 break; \
672 }\
673 if (indx != count) \
674 break; \
675 }
676
677 #define ACKCINODE(opt,neg,val) ACKCICHARS(opt,neg,val,sizeof(val))
678 #define ACKCINAME(opt,neg,val) ACKCICHARS(opt,neg,val,strlen((char *)val))
679
680 #define ACKCINETWORK(opt, neg, val) \
681 if (neg) { \
682 if ((len -= CILEN_NETN) < 0) \
683 break; \
684 GETCHAR(citype, p); \
685 GETCHAR(cilen, p); \
686 if (cilen != CILEN_NETN || \
687 citype != opt) \
688 break; \
689 GETLONG(cilong, p); \
690 if (cilong != val) \
691 break; \
692 }
693
694 #define ACKCIPROTO(opt, neg, val) \
695 if (neg) { \
696 if (len < 2) \
697 break; \
698 GETCHAR(citype, p); \
699 GETCHAR(cilen, p); \
700 if (cilen != CILEN_PROTOCOL || citype != opt) \
701 break; \
702 len -= cilen; \
703 if (len < 0) \
704 break; \
705 GETSHORT(cishort, p); \
706 if (cishort != to_external (val) || cishort == RIP_SAP) \
707 break; \
708 }
709 /*
710 * Process the ACK frame in the order in which the frame was assembled
711 */
712 do {
713 ACKCINETWORK (IPX_NETWORK_NUMBER, go->neg_nn, go->our_network);
714 ACKCINODE (IPX_NODE_NUMBER, go->neg_node, go->our_node);
715 ACKCINAME (IPX_ROUTER_NAME, go->neg_name, go->name);
716 if (len > 0)
717 ACKCIPROTO (IPX_ROUTER_PROTOCOL, go->neg_router, go->router);
718 /*
719 * This is the end of the record.
720 */
721 if (len == 0)
722 return (1);
723 } while (0);
724 /*
725 * The frame is invalid
726 */
727 IPXCPDEBUG(("ipxcp_ackci: received bad Ack!"));
728 return (0);
729 }
730
731 /*
732 * ipxcp_nakci - Peer has sent a NAK for some of our CIs.
733 * This should not modify any state if the Nak is bad
734 * or if IPXCP is in the OPENED state.
735 *
736 * Returns:
737 * 0 - Nak was bad.
738 * 1 - Nak was good.
739 */
740
741 static int
ipxcp_nakci(fsm * f,u_char * p,int len,int treat_as_reject)742 ipxcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
743 {
744 u_char citype, cilen, *next;
745 u_short s;
746 u_int32_t l;
747 ipxcp_options no; /* options we've seen Naks for */
748 ipxcp_options try; /* options to request next time */
749
750 BZERO(&no, sizeof(no));
751 try = *go;
752
753 while (len >= CILEN_VOID) {
754 GETCHAR (citype, p);
755 GETCHAR (cilen, p);
756 len -= cilen;
757 if (cilen < CILEN_VOID || len < 0)
758 goto bad;
759 next = &p [cilen - CILEN_VOID];
760
761 switch (citype) {
762 case IPX_NETWORK_NUMBER:
763 if (!go->neg_nn || no.neg_nn || (cilen != CILEN_NETN))
764 goto bad;
765 no.neg_nn = 1;
766
767 GETLONG(l, p);
768 if (treat_as_reject)
769 try.neg_nn = 0;
770 else if (l && ao->accept_network)
771 try.our_network = l;
772 break;
773
774 case IPX_NODE_NUMBER:
775 if (!go->neg_node || no.neg_node || (cilen != CILEN_NODEN))
776 goto bad;
777 no.neg_node = 1;
778
779 if (treat_as_reject)
780 try.neg_node = 0;
781 else if (!zero_node (p) && ao->accept_local &&
782 ! compare_node (p, ho->his_node))
783 copy_node (p, try.our_node);
784 break;
785
786 /* This has never been sent. Ignore the NAK frame */
787 case IPX_COMPRESSION_PROTOCOL:
788 goto bad;
789
790 case IPX_ROUTER_PROTOCOL:
791 if (!go->neg_router || (cilen < CILEN_PROTOCOL))
792 goto bad;
793
794 GETSHORT (s, p);
795 if (s > 15) /* This is just bad, but ignore for now. */
796 break;
797
798 s = BIT(s);
799 if (no.router & s) /* duplicate NAKs are always bad */
800 goto bad;
801
802 if (no.router == 0) /* Reset on first NAK only */
803 try.router = 0;
804
805 no.router |= s;
806 try.router |= s;
807 try.neg_router = 1;
808 break;
809
810 /* These, according to the RFC, must never be NAKed. */
811 case IPX_ROUTER_NAME:
812 case IPX_COMPLETE:
813 goto bad;
814
815 /* These are for options which we have not seen. */
816 default:
817 break;
818 }
819 p = next;
820 }
821
822 /*
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".
825 */
826 try.router &= (ao->router | BIT(IPX_NONE));
827 if (try.router == 0 && ao->router != 0)
828 try.router = BIT(IPX_NONE);
829
830 if (try.router != 0)
831 try.neg_router = 1;
832
833 /*
834 * OK, the Nak is good. Now we can update state.
835 * If there are any options left, we ignore them.
836 */
837 if (f->state != OPENED)
838 *go = try;
839
840 return 1;
841
842 bad:
843 IPXCPDEBUG(("ipxcp_nakci: received bad Nak!"));
844 return 0;
845 }
846
847 /*
848 * ipxcp_rejci - Reject some of our CIs.
849 */
850 static int
ipxcp_rejci(fsm * f,u_char * p,int len)851 ipxcp_rejci(fsm *f, u_char *p, int len)
852 {
853 u_short cilen, citype, cishort;
854 u_char cichar;
855 u_int32_t cilong;
856 ipxcp_options try; /* options to request next time */
857
858 #define REJCINETWORK(opt, neg, val) \
859 if (neg && p[0] == opt) { \
860 if ((len -= CILEN_NETN) < 0) \
861 break; \
862 GETCHAR(citype, p); \
863 GETCHAR(cilen, p); \
864 if (cilen != CILEN_NETN || \
865 citype != opt) \
866 break; \
867 GETLONG(cilong, p); \
868 if (cilong != val) \
869 break; \
870 neg = 0; \
871 }
872
873 #define REJCICHARS(opt, neg, val, cnt) \
874 if (neg && p[0] == opt) { \
875 int indx, count = cnt; \
876 len -= (count + 2); \
877 if (len < 0) \
878 break; \
879 GETCHAR(citype, p); \
880 GETCHAR(cilen, p); \
881 if (cilen != (count + 2) || \
882 citype != opt) \
883 break; \
884 for (indx = 0; indx < count; ++indx) {\
885 GETCHAR(cichar, p); \
886 if (cichar != ((u_char *) &val)[indx]) \
887 break; \
888 }\
889 if (indx != count) \
890 break; \
891 neg = 0; \
892 }
893
894 #define REJCINODE(opt,neg,val) REJCICHARS(opt,neg,val,sizeof(val))
895 #define REJCINAME(opt,neg,val) REJCICHARS(opt,neg,val,strlen((char *)val))
896
897 #define REJCIVOID(opt, neg) \
898 if (neg && p[0] == opt) { \
899 if ((len -= CILEN_VOID) < 0) \
900 break; \
901 GETCHAR(citype, p); \
902 GETCHAR(cilen, p); \
903 if (cilen != CILEN_VOID || citype != opt) \
904 break; \
905 neg = 0; \
906 }
907
908 /* a reject for RIP/SAP is invalid since we don't send it and you can't
909 reject something which is not sent. (You can NAK, but you can't REJ.) */
910 #define REJCIPROTO(opt, neg, val, bit) \
911 if (neg && p[0] == opt) { \
912 if ((len -= CILEN_PROTOCOL) < 0) \
913 break; \
914 GETCHAR(citype, p); \
915 GETCHAR(cilen, p); \
916 if (cilen != CILEN_PROTOCOL) \
917 break; \
918 GETSHORT(cishort, p); \
919 if (cishort != to_external (val) || cishort == RIP_SAP) \
920 break; \
921 neg = 0; \
922 }
923 /*
924 * Any Rejected CIs must be in exactly the same order that we sent.
925 * Check packet length and CI length at each step.
926 * If we find any deviations, then this packet is bad.
927 */
928 try = *go;
929
930 do {
931 REJCINETWORK (IPX_NETWORK_NUMBER, try.neg_nn, try.our_network);
932 REJCINODE (IPX_NODE_NUMBER, try.neg_node, try.our_node);
933 REJCINAME (IPX_ROUTER_NAME, try.neg_name, try.name);
934 REJCIPROTO (IPX_ROUTER_PROTOCOL, try.neg_router, try.router, 0);
935 /*
936 * This is the end of the record.
937 */
938 if (len == 0) {
939 if (f->state != OPENED)
940 *go = try;
941 return (1);
942 }
943 } while (0);
944 /*
945 * The frame is invalid at this point.
946 */
947 IPXCPDEBUG(("ipxcp_rejci: received bad Reject!"));
948 return 0;
949 }
950
951 /*
952 * ipxcp_reqci - Check the peer's requested CIs and send appropriate response.
953 *
954 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
955 * appropriately. If reject_if_disagree is non-zero, doesn't return
956 * CONFNAK; returns CONFREJ if it can't return CONFACK.
957 */
958 static int
ipxcp_reqci(fsm * f,u_char * inp,int * len,int reject_if_disagree)959 ipxcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
960 {
961 u_char *cip, *next; /* Pointer to current and next CIs */
962 u_short cilen, citype; /* Parsed len, type */
963 u_short cishort; /* Parsed short value */
964 u_int32_t cinetwork; /* Parsed address values */
965 int rc = CONFACK; /* Final packet return code */
966 int orc; /* Individual option return code */
967 u_char *p; /* Pointer to next char to parse */
968 u_char *ucp = inp; /* Pointer to current output char */
969 int l = *len; /* Length left */
970
971 /*
972 * Reset all his options.
973 */
974 BZERO(ho, sizeof(*ho));
975
976 /*
977 * Process all his options.
978 */
979 next = inp;
980 while (l) {
981 orc = CONFACK; /* Assume success */
982 cip = p = next; /* Remember begining of CI */
983 if (l < 2 || /* Not enough data for CI header or */
984 p[1] < 2 || /* CI length too small or */
985 p[1] > l) { /* CI length too big? */
986 IPXCPDEBUG(("ipxcp_reqci: bad CI length!"));
987 orc = CONFREJ; /* Reject bad CI */
988 cilen = l; /* Reject till end of packet */
989 l = 0; /* Don't loop again */
990 goto endswitch;
991 }
992 GETCHAR(citype, p); /* Parse CI type */
993 GETCHAR(cilen, p); /* Parse CI length */
994 l -= cilen; /* Adjust remaining length */
995 next += cilen; /* Step to next CI */
996
997 switch (citype) { /* Check CI type */
998 /*
999 * The network number must match. Choose the larger of the two.
1000 */
1001 case IPX_NETWORK_NUMBER:
1002 /* if we wont negotiate the network number or the length is wrong
1003 then reject the option */
1004 if ( !ao->neg_nn || cilen != CILEN_NETN ) {
1005 orc = CONFREJ;
1006 break;
1007 }
1008 GETLONG(cinetwork, p);
1009
1010 /* If the network numbers match then acknowledge them. */
1011 if (cinetwork != 0) {
1012 ho->his_network = cinetwork;
1013 ho->neg_nn = 1;
1014 if (wo->our_network == cinetwork)
1015 break;
1016 /*
1017 * If the network number is not given or we don't accept their change or
1018 * the network number is too small then NAK it.
1019 */
1020 if (! ao->accept_network || cinetwork < wo->our_network) {
1021 DECPTR (sizeof (u_int32_t), p);
1022 PUTLONG (wo->our_network, p);
1023 orc = CONFNAK;
1024 }
1025 break;
1026 }
1027 /*
1028 * The peer sent '0' for the network. Give it ours if we have one.
1029 */
1030 if (go->our_network != 0) {
1031 DECPTR (sizeof (u_int32_t), p);
1032 PUTLONG (wo->our_network, p);
1033 orc = CONFNAK;
1034 /*
1035 * We don't have one. Reject the value.
1036 */
1037 } else
1038 orc = CONFREJ;
1039
1040 break;
1041 /*
1042 * The node number is required
1043 */
1044 case IPX_NODE_NUMBER:
1045 /* if we wont negotiate the node number or the length is wrong
1046 then reject the option */
1047 if ( cilen != CILEN_NODEN ) {
1048 orc = CONFREJ;
1049 break;
1050 }
1051
1052 copy_node (p, ho->his_node);
1053 ho->neg_node = 1;
1054 /*
1055 * If the remote does not have a number and we do then NAK it with the value
1056 * which we have for it. (We never have a default value of zero.)
1057 */
1058 if (zero_node (ho->his_node)) {
1059 orc = CONFNAK;
1060 copy_node (wo->his_node, p);
1061 INCPTR (sizeof (wo->his_node), p);
1062 break;
1063 }
1064 /*
1065 * If you have given me the expected network node number then I'll accept
1066 * it now.
1067 */
1068 if (compare_node (wo->his_node, ho->his_node)) {
1069 orc = CONFACK;
1070 ho->neg_node = 1;
1071 INCPTR (sizeof (wo->his_node), p);
1072 break;
1073 }
1074 /*
1075 * If his node number is the same as ours then ask him to try the next
1076 * value.
1077 */
1078 if (compare_node (ho->his_node, go->our_node)) {
1079 inc_node (ho->his_node);
1080 orc = CONFNAK;
1081 copy_node (ho->his_node, p);
1082 INCPTR (sizeof (wo->his_node), p);
1083 break;
1084 }
1085 /*
1086 * If we don't accept a new value then NAK it.
1087 */
1088 if (! ao->accept_remote) {
1089 copy_node (wo->his_node, p);
1090 INCPTR (sizeof (wo->his_node), p);
1091 orc = CONFNAK;
1092 break;
1093 }
1094 orc = CONFACK;
1095 ho->neg_node = 1;
1096 INCPTR (sizeof (wo->his_node), p);
1097 break;
1098 /*
1099 * Compression is not desired at this time. It is always rejected.
1100 */
1101 case IPX_COMPRESSION_PROTOCOL:
1102 orc = CONFREJ;
1103 break;
1104 /*
1105 * The routing protocol is a bitmask of various types. Any combination
1106 * of the values RIP_SAP and NLSP are permissible. 'IPX_NONE' for no
1107 * routing protocol must be specified only once.
1108 */
1109 case IPX_ROUTER_PROTOCOL:
1110 if ( !ao->neg_router || cilen < CILEN_PROTOCOL ) {
1111 orc = CONFREJ;
1112 break;
1113 }
1114
1115 GETSHORT (cishort, p);
1116
1117 if (wo->neg_router == 0) {
1118 wo->neg_router = 1;
1119 wo->router = BIT(IPX_NONE);
1120 }
1121
1122 if ((cishort == IPX_NONE && ho->router != 0) ||
1123 (ho->router & BIT(IPX_NONE))) {
1124 orc = CONFREJ;
1125 break;
1126 }
1127
1128 cishort = BIT(cishort);
1129 if (ho->router & cishort) {
1130 orc = CONFREJ;
1131 break;
1132 }
1133
1134 ho->router |= cishort;
1135 ho->neg_router = 1;
1136
1137 /* Finally do not allow a router protocol which we do not
1138 support. */
1139
1140 if ((cishort & (ao->router | BIT(IPX_NONE))) == 0) {
1141 int protocol;
1142
1143 if (cishort == BIT(NLSP) &&
1144 (ao->router & BIT(RIP_SAP)) &&
1145 !wo->tried_rip) {
1146 protocol = RIP_SAP;
1147 wo->tried_rip = 1;
1148 } else
1149 protocol = IPX_NONE;
1150
1151 DECPTR (sizeof (u_int16_t), p);
1152 PUTSHORT (protocol, p);
1153 orc = CONFNAK;
1154 }
1155 break;
1156 /*
1157 * The router name is advisorary. Just accept it if it is not too large.
1158 */
1159 case IPX_ROUTER_NAME:
1160 if (cilen >= CILEN_NAME) {
1161 int name_size = cilen - CILEN_NAME;
1162 if (name_size >= sizeof (ho->name))
1163 name_size = sizeof (ho->name) - 1;
1164 memset (ho->name, 0, sizeof (ho->name));
1165 memcpy (ho->name, p, name_size);
1166 ho->name [name_size] = '\0';
1167 ho->neg_name = 1;
1168 orc = CONFACK;
1169 break;
1170 }
1171 orc = CONFREJ;
1172 break;
1173 /*
1174 * This is advisorary.
1175 */
1176 case IPX_COMPLETE:
1177 if (cilen != CILEN_COMPLETE)
1178 orc = CONFREJ;
1179 else {
1180 ho->neg_complete = 1;
1181 orc = CONFACK;
1182 }
1183 break;
1184 /*
1185 * All other entries are not known at this time.
1186 */
1187 default:
1188 orc = CONFREJ;
1189 break;
1190 }
1191 endswitch:
1192 if (orc == CONFACK && /* Good CI */
1193 rc != CONFACK) /* but prior CI wasnt? */
1194 continue; /* Don't send this one */
1195
1196 if (orc == CONFNAK) { /* Nak this CI? */
1197 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1198 orc = CONFREJ; /* Get tough if so */
1199 if (rc == CONFREJ) /* Rejecting prior CI? */
1200 continue; /* Don't send this one */
1201 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1202 rc = CONFNAK; /* Not anymore... */
1203 ucp = inp; /* Backup */
1204 }
1205 }
1206
1207 if (orc == CONFREJ && /* Reject this CI */
1208 rc != CONFREJ) { /* but no prior ones? */
1209 rc = CONFREJ;
1210 ucp = inp; /* Backup */
1211 }
1212
1213 /* Need to move CI? */
1214 if (ucp != cip)
1215 BCOPY(cip, ucp, cilen); /* Move it */
1216
1217 /* Update output pointer */
1218 INCPTR(cilen, ucp);
1219 }
1220
1221 /*
1222 * If we aren't rejecting this packet, and we want to negotiate
1223 * their address, and they didn't send their address, then we
1224 * send a NAK with a IPX_NODE_NUMBER option appended. We assume the
1225 * input buffer is long enough that we can append the extra
1226 * option safely.
1227 */
1228
1229 if (rc != CONFREJ && !ho->neg_node &&
1230 wo->req_nn && !reject_if_disagree) {
1231 if (rc == CONFACK) {
1232 rc = CONFNAK;
1233 wo->req_nn = 0; /* don't ask again */
1234 ucp = inp; /* reset pointer */
1235 }
1236
1237 if (zero_node (wo->his_node))
1238 inc_node (wo->his_node);
1239
1240 PUTCHAR (IPX_NODE_NUMBER, ucp);
1241 PUTCHAR (CILEN_NODEN, ucp);
1242 copy_node (wo->his_node, ucp);
1243 INCPTR (sizeof (wo->his_node), ucp);
1244 }
1245
1246 *len = ucp - inp; /* Compute output length */
1247 IPXCPDEBUG(("ipxcp: returning Configure-%s", CODENAME(rc)));
1248 return (rc); /* Return final code */
1249 }
1250
1251 /*
1252 * ipxcp_up - IPXCP has come UP.
1253 *
1254 * Configure the IP network interface appropriately and bring it up.
1255 */
1256
1257 static void
ipxcp_up(fsm * f)1258 ipxcp_up(fsm *f)
1259 {
1260 int unit = f->unit;
1261
1262 IPXCPDEBUG(("ipxcp: up"));
1263
1264 /* The default router protocol is RIP/SAP. */
1265 if (ho->router == 0)
1266 ho->router = BIT(RIP_SAP);
1267
1268 if (go->router == 0)
1269 go->router = BIT(RIP_SAP);
1270
1271 /* Fetch the network number */
1272 if (!ho->neg_nn)
1273 ho->his_network = wo->his_network;
1274
1275 if (!ho->neg_node)
1276 copy_node (wo->his_node, ho->his_node);
1277
1278 if (!wo->neg_node && !go->neg_node)
1279 copy_node (wo->our_node, go->our_node);
1280
1281 if (zero_node (go->our_node)) {
1282 static char errmsg[] = "Could not determine local IPX node address";
1283 if (debug)
1284 error(errmsg);
1285 ipxcp_close(f->unit, errmsg);
1286 return;
1287 }
1288
1289 go->network = go->our_network;
1290 if (ho->his_network != 0 && ho->his_network > go->network)
1291 go->network = ho->his_network;
1292
1293 if (go->network == 0) {
1294 static char errmsg[] = "Can not determine network number";
1295 if (debug)
1296 error(errmsg);
1297 ipxcp_close (unit, errmsg);
1298 return;
1299 }
1300
1301 /* bring the interface up */
1302 if (!sifup(unit)) {
1303 if (debug)
1304 warn("sifup failed (IPX)");
1305 ipxcp_close(unit, "Interface configuration failed");
1306 return;
1307 }
1308 ipxcp_is_up = 1;
1309
1310 /* set the network number for IPX */
1311 if (!sipxfaddr(unit, go->network, go->our_node)) {
1312 if (debug)
1313 warn("sipxfaddr failed");
1314 ipxcp_close(unit, "Interface configuration failed");
1315 return;
1316 }
1317
1318 np_up(f->unit, PPP_IPX);
1319
1320 /*
1321 * Execute the ipx-up script, like this:
1322 * /etc/ppp/ipx-up interface tty speed local-IPX remote-IPX
1323 */
1324
1325 ipxcp_script (f, _PATH_IPXUP);
1326 }
1327
1328 /*
1329 * ipxcp_down - IPXCP has gone DOWN.
1330 *
1331 * Take the IP network interface down, clear its addresses
1332 * and delete routes through it.
1333 */
1334
1335 static void
ipxcp_down(fsm * f)1336 ipxcp_down(fsm *f)
1337 {
1338 IPXCPDEBUG(("ipxcp: down"));
1339
1340 if (!ipxcp_is_up)
1341 return;
1342 ipxcp_is_up = 0;
1343 np_down(f->unit, PPP_IPX);
1344 cipxfaddr(f->unit);
1345 sifnpmode(f->unit, PPP_IPX, NPMODE_DROP);
1346 sifdown(f->unit);
1347 ipxcp_script (f, _PATH_IPXDOWN);
1348 }
1349
1350
1351 /*
1352 * ipxcp_finished - possibly shut down the lower layers.
1353 */
1354 static void
ipxcp_finished(fsm * f)1355 ipxcp_finished(fsm *f)
1356 {
1357 np_finished(f->unit, PPP_IPX);
1358 }
1359
1360
1361 /*
1362 * ipxcp_script - Execute a script with arguments
1363 * interface-name tty-name speed local-IPX remote-IPX networks.
1364 */
1365 static void
ipxcp_script(fsm * f,char * script)1366 ipxcp_script(fsm *f, char *script)
1367 {
1368 char strspeed[32], strlocal[32], strremote[32];
1369 char strnetwork[32], strpid[32];
1370 char *argv[14], strproto_lcl[32], strproto_rmt[32];
1371
1372 slprintf(strpid, sizeof(strpid), "%d", getpid());
1373 slprintf(strspeed, sizeof(strspeed),"%d", baud_rate);
1374
1375 strproto_lcl[0] = '\0';
1376 if (go->neg_router && ((go->router & BIT(IPX_NONE)) == 0)) {
1377 if (go->router & BIT(RIP_SAP))
1378 strlcpy (strproto_lcl, "RIP ", sizeof(strproto_lcl));
1379 if (go->router & BIT(NLSP))
1380 strlcat (strproto_lcl, "NLSP ", sizeof(strproto_lcl));
1381 }
1382
1383 if (strproto_lcl[0] == '\0')
1384 strlcpy (strproto_lcl, "NONE ", sizeof(strproto_lcl));
1385
1386 strproto_lcl[strlen (strproto_lcl)-1] = '\0';
1387
1388 strproto_rmt[0] = '\0';
1389 if (ho->neg_router && ((ho->router & BIT(IPX_NONE)) == 0)) {
1390 if (ho->router & BIT(RIP_SAP))
1391 strlcpy (strproto_rmt, "RIP ", sizeof(strproto_rmt));
1392 if (ho->router & BIT(NLSP))
1393 strlcat (strproto_rmt, "NLSP ", sizeof(strproto_rmt));
1394 }
1395
1396 if (strproto_rmt[0] == '\0')
1397 strlcpy (strproto_rmt, "NONE ", sizeof(strproto_rmt));
1398
1399 strproto_rmt[strlen (strproto_rmt)-1] = '\0';
1400
1401 strlcpy (strnetwork, ipx_ntoa (go->network), sizeof(strnetwork));
1402
1403 slprintf (strlocal, sizeof(strlocal), "%0.6B", go->our_node);
1404
1405 slprintf (strremote, sizeof(strremote), "%0.6B", ho->his_node);
1406
1407 argv[0] = script;
1408 argv[1] = ifname;
1409 argv[2] = devnam;
1410 argv[3] = strspeed;
1411 argv[4] = strnetwork;
1412 argv[5] = strlocal;
1413 argv[6] = strremote;
1414 argv[7] = strproto_lcl;
1415 argv[8] = strproto_rmt;
1416 argv[9] = (char *)go->name;
1417 argv[10] = (char *)ho->name;
1418 argv[11] = ipparam;
1419 argv[12] = strpid;
1420 argv[13] = NULL;
1421 run_program(script, argv, 0, NULL, NULL, 0);
1422 }
1423
1424 /*
1425 * ipxcp_printpkt - print the contents of an IPXCP packet.
1426 */
1427 static char *ipxcp_codenames[] = {
1428 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1429 "TermReq", "TermAck", "CodeRej"
1430 };
1431
1432 static int
ipxcp_printpkt(u_char * p,int plen,void (* printer)(void *,char *,...),void * arg)1433 ipxcp_printpkt(u_char *p, int plen,
1434 void (*printer) (void *, char *, ...), void *arg)
1435 {
1436 int code, id, len, olen;
1437 u_char *pstart, *optend;
1438 u_short cishort;
1439 u_int32_t cilong;
1440
1441 if (plen < HEADERLEN)
1442 return 0;
1443 pstart = p;
1444 GETCHAR(code, p);
1445 GETCHAR(id, p);
1446 GETSHORT(len, p);
1447 if (len < HEADERLEN || len > plen)
1448 return 0;
1449
1450 if (code >= 1 && code <= sizeof(ipxcp_codenames) / sizeof(char *))
1451 printer(arg, " %s", ipxcp_codenames[code-1]);
1452 else
1453 printer(arg, " code=0x%x", code);
1454 printer(arg, " id=0x%x", id);
1455 len -= HEADERLEN;
1456 switch (code) {
1457 case CONFREQ:
1458 case CONFACK:
1459 case CONFNAK:
1460 case CONFREJ:
1461 /* print option list */
1462 while (len >= 2) {
1463 GETCHAR(code, p);
1464 GETCHAR(olen, p);
1465 p -= 2;
1466 if (olen < CILEN_VOID || olen > len) {
1467 break;
1468 }
1469 printer(arg, " <");
1470 len -= olen;
1471 optend = p + olen;
1472 switch (code) {
1473 case IPX_NETWORK_NUMBER:
1474 if (olen == CILEN_NETN) {
1475 p += 2;
1476 GETLONG(cilong, p);
1477 printer (arg, "network %s", ipx_ntoa (cilong));
1478 }
1479 break;
1480 case IPX_NODE_NUMBER:
1481 if (olen == CILEN_NODEN) {
1482 p += 2;
1483 printer (arg, "node ");
1484 while (p < optend) {
1485 GETCHAR(code, p);
1486 printer(arg, "%.2x", (int) (unsigned int) (unsigned char) code);
1487 }
1488 }
1489 break;
1490 case IPX_COMPRESSION_PROTOCOL:
1491 if (olen == CILEN_COMPRESS) {
1492 p += 2;
1493 GETSHORT (cishort, p);
1494 printer (arg, "compression %d", (int) cishort);
1495 }
1496 break;
1497 case IPX_ROUTER_PROTOCOL:
1498 if (olen == CILEN_PROTOCOL) {
1499 p += 2;
1500 GETSHORT (cishort, p);
1501 printer (arg, "router proto %d", (int) cishort);
1502 }
1503 break;
1504 case IPX_ROUTER_NAME:
1505 if (olen >= CILEN_NAME) {
1506 p += 2;
1507 printer (arg, "router name \"");
1508 while (p < optend) {
1509 GETCHAR(code, p);
1510 if (code >= 0x20 && code <= 0x7E)
1511 printer (arg, "%c", (int) (unsigned int) (unsigned char) code);
1512 else
1513 printer (arg, " \\%.2x", (int) (unsigned int) (unsigned char) code);
1514 }
1515 printer (arg, "\"");
1516 }
1517 break;
1518 case IPX_COMPLETE:
1519 if (olen == CILEN_COMPLETE) {
1520 p += 2;
1521 printer (arg, "complete");
1522 }
1523 break;
1524 default:
1525 break;
1526 }
1527
1528 while (p < optend) {
1529 GETCHAR(code, p);
1530 printer(arg, " %.2x", (int) (unsigned int) (unsigned char) code);
1531 }
1532 printer(arg, ">");
1533 }
1534 break;
1535
1536 case TERMACK:
1537 case TERMREQ:
1538 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1539 printer(arg, " ");
1540 print_string((char *)p, len, printer, arg);
1541 p += len;
1542 len = 0;
1543 }
1544 break;
1545 }
1546
1547 /* print the rest of the bytes in the packet */
1548 for (; len > 0; --len) {
1549 GETCHAR(code, p);
1550 printer(arg, " %.2x", (int) (unsigned int) (unsigned char) code);
1551 }
1552
1553 return p - pstart;
1554 }
1555 #endif /* ifdef IPX_CHANGE */
1556