2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1987, 1988, 1989 Apple Computer, Inc.
26 * Modified for MP, 1996 by Tuyen Nguyen
27 * Added AURP support, April 8, 1996 by Tuyen Nguyen
28 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
31 #define RESOLVE_DBG /* define debug globals in debug.h */
33 #include <sys/errno.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <machine/spl.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/filedesc.h>
41 #include <sys/fcntl.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/protosw.h>
52 #include <netat/sysglue.h>
53 #include <netat/appletalk.h>
54 #include <netat/at_var.h>
55 #include <netat/ddp.h>
57 #include <netat/nbp.h>
58 #include <netat/rtmp.h>
59 #include <netat/zip.h>
60 #include <netat/at_pcb.h>
61 #include <netat/routing_tables.h>
62 #include <netat/at_snmp.h>
63 #include <netat/aurp.h>
64 #include <netat/debug.h>
65 #include <netat/at_ddp_brt.h>
66 #include <netat/at_aarp.h>
67 #include <netat/adsp.h>
68 #include <netat/adsp_internal.h>
72 /* Queue of LAP interfaces which have registered themselves with DDP */
73 struct at_ifQueueHd at_ifQueueHd
;
75 extern at_state_t at_state
;
76 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
78 snmpStats_t snmpStats
; /* snmp ddp & echo stats */
80 extern at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
81 extern struct atpcb ddp_head
;
82 extern at_ifaddr_t
*ifID_home
, *ifID_table
[];
83 extern aarp_amt_array
*aarp_table
[];
84 extern at_ifaddr_t at_interfaces
[];
86 /* routing mode special */
87 void (*ddp_AURPsendx
)();
88 at_ifaddr_t
*aurp_ifID
= 0;
89 extern pktsIn
,pktsOut
;
90 int pktsDropped
,pktsHome
;
95 extern int *adsp_pidM
;
96 extern struct atpcb
*atp_inputQ
[];
97 extern CCB
*adsp_inputQ
[];
99 at_ifaddr_t
*forUs(at_ddp_t
*);
101 void ddp_input(), ddp_notify_nbp();
103 extern void routing_needed();
104 extern void ddp_brt_sweep();
110 void init_ddp_handler()
112 bzero(ddp_handler
, sizeof(ddp_handler
));
115 void add_ddp_handler(ddp_socket
, input_func
)
117 void (*input_func
)();
119 ddp_handler
[ddp_socket
].func
= input_func
;
129 * Raw DDP socket option processing.
131 int ddp_ctloutput(so
, sopt
)
133 struct sockopt
*sopt
;
135 struct atpcb
*at_pcb
= sotoatpcb(so
);
136 int optval
, error
= 0;
138 if (sopt
->sopt_level
!= ATPROTO_NONE
)
141 switch (sopt
->sopt_dir
) {
144 switch (sopt
->sopt_name
) {
146 optval
= at_pcb
->ddp_flags
& DDPFLG_HDRINCL
;
147 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
150 optval
= at_pcb
->ddp_flags
& DDPFLG_CHKSUM
;
151 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
154 optval
= at_pcb
->ddp_flags
& DDPFLG_STRIPHDR
;
155 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
158 optval
= at_pcb
->ddp_flags
& DDPFLG_SLFSND
;
159 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
161 case DDP_GETSOCKNAME
:
164 addr
.inet
.net
= at_pcb
->laddr
.s_net
;
165 addr
.inet
.node
= at_pcb
->laddr
.s_node
;
166 addr
.inet
.socket
= at_pcb
->lport
;
167 addr
.ddptype
= at_pcb
->ddptype
;
168 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
177 switch (sopt
->sopt_name
) {
179 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
184 at_pcb
->ddp_flags
|= DDPFLG_HDRINCL
;
186 at_pcb
->ddp_flags
&= ~DDPFLG_HDRINCL
;
189 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
194 at_pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
196 at_pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
199 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
204 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
206 at_pcb
->ddp_flags
&= ~DDPFLG_STRIPHDR
;
209 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
214 at_pcb
->ddp_flags
|= DDPFLG_SLFSND
;
216 at_pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
228 /****************************************************************/
231 /* Support Routines */
234 /****************************************************************/
241 * This procedure determines the checksum of an extended DDP datagram.
242 * Add the unsigned bytes into an unsigned 16-bit accumulator.
243 * After each add, rotate the sign bit into the low order bit of
244 * the accumulator. When done, if the checksum is 0, changed into 0xFFFF.
247 * checksum = ddp_checksum(mp, offset)
250 * mp pointer to the datagram gbuf_t
251 * offset offset to start at in first gbuf_t block
258 u_short
ddp_checksum(mp
, offset
)
262 register u_char
*data
;
264 register u_short checksum
;
269 if (offset
>= gbuf_len(mp
))
270 offset
-= gbuf_len(mp
);
272 data
= ((unsigned char *) gbuf_rptr(mp
)) + offset
;
273 length
= gbuf_len(mp
) - offset
;
275 /* Portable checksum from 3.0 */
278 checksum
= (checksum
& 0x8000) ?
279 ((checksum
<< 1) | 1) : (checksum
<< 1);
282 } while ( (mp
= gbuf_cont(mp
)) );
294 * This procedure is called by each LAP interface when it wants to place
295 * itself online. The LAP interfaces passes in a pointer to its at_if
296 * struct, which is added to DDP's list of active interfaces (at_ifQueueHd).
297 * When DDP wants to transmit a packet, it searches this list for the
300 * If AT_IFF_DEFAULT is set, then this interface is to be brought online
301 * as the interface DDP socket addresses are tied to. Of course there can
302 * be only one default interface; we return an error if it's already set.
305 * ret_status = ddp_add_if(ifID)
308 * ifID pointer to LAP interface's at_if struct.
311 * 0 Procedure successfully completed.
312 * EALREADY This interface is already online, or there is
313 * already a default interface.
314 * ENOBUFS Cannot allocate input queue
318 register at_ifaddr_t
*ifID
;
322 dPrintf(D_M_DDP
, D_L_STARTUP
,
323 ("ddp_add_if: called, ifID:0x%x\n", (u_int
) ifID
));
325 if (ifID
->ifFlags
& AT_IFF_DEFAULT
) {
327 return(EEXIST
); /* home port already set */
333 for (port
=IFID_HOME
+1; port
<IF_TOTAL_MAX
; port
++)
334 if (!ifID_table
[port
]) {
337 if (port
== IF_TOTAL_MAX
) /* no space left */
341 /* allocate an et_aarp_amt structure */
342 if ((aarp_table
[port
] =
343 (aarp_amt_array
*)_MALLOC(sizeof(aarp_amt_array
),
344 M_RTABLE
, M_WAITOK
)) == NULL
)
347 dPrintf(D_M_DDP
, D_L_STARTUP
, ("ddp:adding ifID_table[%d]\n", port
));
349 /* add i/f to port list */
350 ifID_table
[port
] = ifID
;
351 ifID
->ifPort
= port
; /* set ddp port # in ifID */
353 /* Add this interface to the list of online interfaces */
354 TAILQ_INSERT_TAIL(&at_ifQueueHd
, ifID
, aa_link
);
363 * This procedure is called by each LAP interface when it wants to take
364 * itself offline. The LAP interfaces passes in a pointer to its at_if
365 * struct; DDP's list of active interfaces (at_ifQueueHd) is searched and
366 * this interface is removed from the list. DDP can still transmit
367 * packets as long as this interface is not the default interface; the
368 * sender will just get ENETUNREACH errors when it tries to send to an
369 * interface that went offline. However, if the default interface is
370 * taken offline, we no longer have a node ID to use as a source address
371 * and DDP must return ENETDOWN when a caller tries to send a packet.
374 * ifID pointer to LAP interface's at_if struct.
377 void ddp_rem_if(ifID
)
378 register at_ifaddr_t
*ifID
;
380 struct ifaddr
*ifa
= &ifID
->aa_ifa
;
382 /* un-do processing done in SIOCSIFADDR */
385 TAILQ_REMOVE(&ifID
->aa_ifp
->if_addrhead
, ifa
, ifa_link
);
386 ifa
->ifa_addr
= NULL
;
389 if (ifID
->at_dl_tag
) {
390 /* dlil_detach_protocol(ifID->at_dl_tag); */
391 ether_detach_at(ifID
->aa_ifp
);
395 /* un-do processing done in ddp_add_if() */
397 if (aarp_table
[ifID
->ifPort
]) {
398 FREE(aarp_table
[ifID
->ifPort
], M_RTABLE
);
399 aarp_table
[ifID
->ifPort
] = NULL
;
402 at_state
.flags
|= AT_ST_IF_CHANGED
;
405 trackrouter_rem_if(ifID
);
406 TAILQ_REMOVE(&at_ifQueueHd
, ifID
, aa_link
);
407 ifID_table
[ifID
->ifPort
] = NULL
;
408 ifID
->ifName
[0] = '\0';
412 /* *** deallocate ifID, eventually *** */
416 * The user may have registered an NVE with the NBP on a socket. When the
417 * socket is closed, the NVE should be deleted from NBP's name table. The
418 * user should delete the NVE before the socket is shut down, but there
419 * may be circumstances when he can't. So, whenever a DDP socket is closed,
420 * this routine is used to notify NBP of the socket closure. This would
421 * help NBP get rid of all NVE's registered on the socket.
424 /* *** Do we still need to do this? *** */
425 int ot_ddp_check_socket(socket
, pid
)
426 unsigned char socket
;
432 dPrintf(D_M_DDP
, D_L_INFO
, ("ot_ddp_check_socket: %d\n", socket
));
433 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
; gref
= gref
->atpcb_next
)
434 if (gref
->lport
== socket
&& gref
->pid
== pid
)
436 if ((atp_inputQ
[socket
] != NULL
) && (atp_inputQ
[socket
] != (gref_t
*)1)
437 && (atp_pidM
[socket
] == pid
))
439 if ((adsp_inputQ
[socket
] != NULL
) && (adsp_pidM
[socket
] == pid
))
445 void ddp_notify_nbp(socket
, pid
, ddptype
)
446 unsigned char socket
;
448 unsigned char ddptype
; /* not used */
451 nve_entry_t
*nve_entry
, *nve_next
;
453 if (at_state
.flags
& AT_ST_STARTED
) {
454 /* *** NBP_CLOSE_NOTE processing (from ddp_nbp.c) *** */
455 ATDISABLE(nve_lock
, NVE_LOCK
);
456 for ((nve_entry
= TAILQ_FIRST(&name_registry
)); nve_entry
; nve_entry
= nve_next
) {
457 nve_next
= TAILQ_NEXT(nve_entry
, nve_link
);
458 if ((at_socket
)socket
== nve_entry
->address
.socket
&&
459 /* *** check complete address and ddptype here *** */
460 pid
== nve_entry
->pid
&&
461 ot_ddp_check_socket(nve_entry
->address
.socket
,
462 nve_entry
->pid
) < 2) {
463 /* NB: nbp_delete_entry calls TAILQ_REMOVE */
464 nbp_delete_entry(nve_entry
);
467 ATENABLE(nve_lock
, NVE_LOCK
);
469 } /* ddp_notify_nbp */
471 static void fillin_pkt_chain(m
)
476 *ddp
= (at_ddp_t
*)gbuf_rptr(m
),
480 if (UAS_VALUE(ddp
->checksum
)) {
481 tmp
= ddp_checksum(m
, 4);
482 UAS_ASSIGN(ddp
->checksum
, tmp
);
485 for (tmp_m
=gbuf_next(tmp_m
); tmp_m
; tmp_m
=gbuf_next(tmp_m
)) {
486 tmp_ddp
= (at_ddp_t
*)gbuf_rptr(tmp_m
);
487 tmp_ddp
->length
= gbuf_msgsize(tmp_m
);
490 NET_NET(tmp_ddp
->src_net
, ddp
->src_net
);
491 tmp_ddp
->src_node
= ddp
->src_node
;
492 tmp_ddp
->src_socket
= ddp
->src_socket
;
493 if (UAS_VALUE(tmp_ddp
->checksum
)) {
494 tmp
= ddp_checksum(tmp_m
, 4);
495 UAS_ASSIGN(tmp_ddp
->checksum
, tmp
);
500 /* There are various ways a packet may go out.... it may be sent out
501 * directly to destination node, or sent to a random router or sent
502 * to a router whose entry exists in Best Router Cache. Following are
503 * constants used WITHIN this routine to keep track of choice of destination
505 #define DIRECT_ADDR 1
507 #define BRIDGE_ADDR 3
513 * Called to queue a atp/ddp data packet on the network interface.
514 * It returns 0 normally, and an errno in case of error.
515 * The mbuf chain pointed to by *mp is consumed on success, and
516 * freed in case of error.
519 int ddp_output(mp
, src_socket
, src_addr_included
)
520 register gbuf_t
**mp
;
521 at_socket src_socket
;
522 int src_addr_included
;
524 register at_ifaddr_t
*ifID
= ifID_home
, *ifIDTmp
= NULL
;
525 register at_ddp_t
*ddp
;
526 register ddp_brt_t
*brt
;
527 register at_net_al dst_net
;
529 struct atalk_addr at_dest
;
530 at_ifaddr_t
*ARouterIf
= NULL
;
538 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_START
, 0,
541 snmpStats
.dd_outReq
++;
544 ddp
= (at_ddp_t
*)gbuf_rptr(m
);
546 if ((ddp
->dst_socket
> (unsigned) (DDP_SOCKET_LAST
+ 1)) ||
547 (ddp
->dst_socket
< DDP_SOCKET_1st_RESERVED
)) {
548 dPrintf(D_M_DDP
, D_L_ERROR
,
549 ("Illegal destination socket on outgoing packet (0x%x)",
551 at_ddp_stats
.xmit_bad_addr
++;
554 goto exit_ddp_output
;
556 if ((len
= gbuf_msgsize(*mp
)) > DDP_DATAGRAM_SIZE
) {
557 /* the packet is too large */
558 dPrintf(D_M_DDP
, D_L_ERROR
,
559 ("Outgoing packet too long (len=%d bytes)", len
));
560 at_ddp_stats
.xmit_bad_length
++;
563 goto exit_ddp_output
;
565 at_ddp_stats
.xmit_bytes
+= len
;
566 at_ddp_stats
.xmit_packets
++;
572 /* If this packet is for the same node, loop it back
573 * up... Note that for LocalTalk, dst_net zero means "THIS_NET", so
574 * address 0.nn is eligible for loopback. For Extended EtherTalk,
575 * dst_net 0 can be used only for cable-wide or zone-wide
576 * broadcasts (0.ff) and as such, address of the form 0.nn is NOT
577 * eligible for loopback.
579 dst_net
= NET_VALUE(ddp
->dst_net
);
581 /* If our packet is destined for the 'virtual' bridge
582 * address of NODE==0xFE, replace that address with a
583 * real bridge address.
585 if ((ddp
->dst_node
== 0xfe) &&
586 ((dst_net
== ATADDR_ANYNET
) ||
587 (dst_net
>= ifID_home
->ifThisCableStart
&&
588 dst_net
<= ifID_home
->ifThisCableEnd
))) {
589 /* if there's a router that's not us, it's in ifID_home */
590 NET_ASSIGN(ddp
->dst_net
, ifID_home
->ifARouter
.s_net
);
591 dst_net
= ifID_home
->ifARouter
.s_net
;
592 ddp
->dst_node
= ifID_home
->ifARouter
.s_node
;
595 if (MULTIHOME_MODE
&& (ifIDTmp
= forUs(ddp
))) {
598 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
599 ("ddp_out: for us if:%s\n", ifIDTmp
->ifName
));
603 loop
= ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
604 (dst_net
== ifID
->ifThisNode
.s_net
)
607 gbuf_t
*mdata
, *mdata_next
;
609 if (!MULTIHOME_MODE
|| !src_addr_included
) {
610 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
611 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
613 ddp
->src_socket
= src_socket
;
615 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
616 ("ddp_output: loop to %d:%d port=%d\n",
617 NET_VALUE(ddp
->dst_net
),
621 fillin_pkt_chain(*mp
);
623 dPrintf(D_M_DDP
, D_L_VERBOSE
,
624 ("Looping back packet from skt 0x%x to skt 0x%x\n",
625 ddp
->src_socket
, ddp
->dst_socket
));
627 for (mdata
= *mp
; mdata
; mdata
= mdata_next
) {
628 mdata_next
= gbuf_next(mdata
);
629 gbuf_next(mdata
) = 0;
630 ddp_input(mdata
, ifID
);
632 goto exit_ddp_output
;
634 if ((ddp
->dst_socket
== ZIP_SOCKET
) &&
635 (zip_type_packet(*mp
) == ZIP_GETMYZONE
)) {
636 ddp
->src_socket
= src_socket
;
637 error
= zip_handle_getmyzone(ifID
, *mp
);
639 goto exit_ddp_output
;
642 * find out the interface on which the packet should go out
644 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
645 if ((ifID
->ifThisNode
.s_net
== dst_net
) || (dst_net
== 0))
646 /* the message is either going out (i) on the same
647 * NETWORK in case of LocalTalk, or (ii) on the same
648 * CABLE in case of Extended AppleTalk (EtherTalk).
652 if ((ifID
->ifThisCableStart
<= dst_net
) &&
653 (ifID
->ifThisCableEnd
>= dst_net
)
655 /* We're on EtherTalk and the message is going out to
656 * some other network on the same cable.
660 if (ARouterIf
== NULL
&& ATALK_VALUE(ifID
->ifARouter
))
663 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
664 ("ddp_output: after search ifid:0x%x %s ifID_home:0x%x\n",
665 (u_int
)ifID
, ifID
? ifID
->ifName
: "",
669 /* located the interface where the packet should
670 * go.... the "first-hop" destination address
671 * must be the same as real destination address.
673 addr_type
= DIRECT_ADDR
;
675 /* no, the destination network number does
676 * not match known network numbers. If we have
677 * heard from this network recently, BRT table
678 * may have address of a router we could use!
680 if (!MULTIPORT_MODE
) {
681 BRT_LOOK (brt
, dst_net
);
683 /* Bingo... BRT has an entry for this network.
684 * Use the link address as is.
686 dPrintf(D_M_DDP
, D_L_VERBOSE
,
687 ("Found BRT entry to send to net 0x%x", dst_net
));
688 at_ddp_stats
.xmit_BRT_used
++;
689 addr_type
= BRT_ENTRY
;
692 /* No BRT entry available for dest network... do we
693 * know of any router at all??
695 if ((ifID
= ARouterIf
) != NULL
)
696 addr_type
= BRIDGE_ADDR
;
698 dPrintf(D_M_DDP
, D_L_WARNING
,
699 ("Found no interface to send pkt"));
700 at_ddp_stats
.xmit_bad_addr
++;
703 goto exit_ddp_output
;
707 else { /* We are in multiport mode, so we can bypass all the rest
708 * and directly ask for the routing of the packet
710 at_ddp_stats
.xmit_BRT_used
++;
713 if (!src_addr_included
) {
714 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
715 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
717 ddp
->src_socket
= src_socket
;
718 routing_needed(*mp
, ifID
, TRUE
);
720 goto exit_ddp_output
;
723 /* by the time we land here, we know the interface on
724 * which this packet is going out.... ifID.
726 if (ifID
->ifState
== LAP_OFFLINE
) {
728 goto exit_ddp_output
;
734 at_dest.atalk_unused = 0;
736 NET_ASSIGN(at_dest
.atalk_net
, dst_net
);
737 at_dest
.atalk_node
= ddp
->dst_node
;
739 addr
= (char *)&at_dest
;
743 addr
= (char *)&brt
->et_addr
;
746 NET_ASSIGN(at_dest
.atalk_net
, ifID
->ifARouter
.s_net
);
747 at_dest
.atalk_node
= ifID
->ifARouter
.s_node
;
749 addr
= (char *)&at_dest
;
753 /* Irrespective of the interface on which
754 * the packet is going out, we always put the
755 * same source address on the packet (unless multihoming mode).
757 if (MULTIHOME_MODE
) {
758 if (!src_addr_included
) {
759 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
760 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
764 ddp
->src_node
= ifID_home
->ifThisNode
.s_node
;
765 NET_ASSIGN(ddp
->src_net
, ifID_home
->ifThisNode
.s_net
);
767 ddp
->src_socket
= src_socket
;
769 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
770 ("ddp_output: going out to %d:%d skt%d on %s\n",
771 dst_net
, ddp
->dst_node
, ddp
->dst_socket
, ifID
->ifName
));
773 fillin_pkt_chain(*mp
);
776 struct etalk_addr dest_addr
;
777 struct atalk_addr dest_at_addr
;
778 int loop
= TRUE
; /* flag to aarp to loopback (default) */
782 /* the incoming frame is of the form {flag, address, ddp...}
783 * where "flag" indicates whether the address is an 802.3
784 * (link) address, or an appletalk address. If it's an
785 * 802.3 address, the packet can just go out to the network
786 * through PAT, if it's an appletalk address, AT->802.3 address
787 * resolution needs to be done.
788 * If 802.3 address is known, strip off the flag and 802.3
789 * address, and prepend 802.2 and 802.3 headers.
793 addr_flag
= *(u_char
*)gbuf_rptr(m
);
798 case AT_ADDR_NO_LOOP
:
803 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
804 gbuf_rinc(m
,sizeof(struct atalk_addr
));
806 dest_at_addr
= *(struct atalk_addr
*)addr
;
810 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
811 gbuf_rinc(m
,sizeof(struct etalk_addr
));
813 dest_addr
= *(struct etalk_addr
*)addr
;
816 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
817 ("ddp_output: Unknown addr_flag = 0x%x\n", addr_flag
));
818 gbuf_freel(m
); /* unknown address type, chuck it */
819 goto exit_ddp_output
;
824 /* At this point, rptr points to ddp header for sure */
825 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
826 /* see if this is a ZIP packet that we need
827 * to let through even though network is
830 if (zip_type_packet(m
) == 0) {
832 goto exit_ddp_output
;
836 ifID
->stats
.xmit_packets
++;
837 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
838 snmpStats
.dd_outLong
++;
841 case AT_ADDR_NO_LOOP
:
844 * we don't want elap to be looking into ddp header, so
845 * it doesn't know net#, consequently can't do
846 * AMT_LOOKUP. That task left to aarp now.
848 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
851 pat_output(ifID
, m
, &dest_addr
, 0);
856 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_END
, 0,
861 void ddp_input(mp
, ifID
)
863 register at_ifaddr_t
*ifID
;
865 register at_ddp_t
*ddp
; /* DDP header */
866 register int msgsize
;
867 register at_socket socket
;
869 register at_net_al dst_net
;
871 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_START
, 0,
872 ifID
, mp
, gbuf_len(mp
),0);
874 /* Makes sure we know the default interface before starting to
875 * accept incomming packets. If we don't we may end up with a
876 * null ifID_table[0] and have impredicable results (specially
877 * in router mode. This is a transitory state (because we can
878 * begin to receive packet while we're not completly set up yet.
881 if (ifID_home
== (at_ifaddr_t
*)NULL
) {
882 dPrintf(D_M_DDP
, D_L_ERROR
,
883 ("dropped incoming packet ifID_home not set yet\n"));
885 goto out
; /* return */
889 * if a DDP packet has been broadcast, we're going to get a copy of
890 * it here; if it originated at user level via a write on a DDP
891 * socket; when it gets here, the first block in the chain will be
892 * empty since it only contained the lap level header which will be
893 * stripped in the lap level immediately below ddp
896 if ((mp
= (gbuf_t
*)ddp_compress_msg(mp
)) == NULL
) {
897 dPrintf(D_M_DDP
, D_L_ERROR
,
898 ("dropped short incoming ET packet (len %d)", 0));
899 snmpStats
.dd_inTotal
++;
900 at_ddp_stats
.rcv_bad_length
++;
901 goto out
; /* return; */
903 msgsize
= gbuf_msgsize(mp
);
905 at_ddp_stats
.rcv_bytes
+= msgsize
;
906 at_ddp_stats
.rcv_packets
++;
908 /* if the interface pointer is 0, the packet has been
909 * looped back by 'write' half of DDP. It is of the
910 * form {extended ddp,...}. The packet is meant to go
911 * up to some socket on the same node.
913 if (!ifID
) /* if loop back is specified */
914 ifID
= ifID_home
; /* that means the home port */
916 /* the incoming datagram has extended DDP header and is of
917 * the form {ddp,...}.
919 if (msgsize
< DDP_X_HDR_SIZE
) {
920 dPrintf(D_M_DDP
, D_L_ERROR
,
921 ("dropped short incoming ET packet (len %d)", msgsize
));
922 at_ddp_stats
.rcv_bad_length
++;
924 goto out
; /* return; */
927 * At this point, the message is always of the form
928 * {extended ddp, ... }.
930 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
933 if (msgsize
!= len
) {
934 if ((unsigned) msgsize
> len
) {
935 if (len
< DDP_X_HDR_SIZE
) {
936 dPrintf(D_M_DDP
, D_L_ERROR
,
937 ("Length problems, ddp length %d, buffer length %d",
939 snmpStats
.dd_tooLong
++;
940 at_ddp_stats
.rcv_bad_length
++;
942 goto out
; /* return; */
945 * shave off the extra bytes from the end of message
947 mp
= ddp_adjmsg(mp
, -(msgsize
- len
)) ? mp
: 0;
949 goto out
; /* return; */
951 dPrintf(D_M_DDP
, D_L_ERROR
,
952 ("Length problems, ddp length %d, buffer length %d",
954 snmpStats
.dd_tooShort
++;
955 at_ddp_stats
.rcv_bad_length
++;
957 goto out
; /* return; */
960 socket
= ddp
->dst_socket
;
963 * We want everything in router mode, specially socket 254 for nbp so we need
964 * to bypass this test when we are a router.
967 if (!MULTIPORT_MODE
&& (socket
> DDP_SOCKET_LAST
||
968 socket
< DDP_SOCKET_1st_RESERVED
)) {
969 dPrintf(D_M_DDP
, D_L_WARNING
,
970 ("Bad dst socket on incoming packet (0x%x)",
972 at_ddp_stats
.rcv_bad_socket
++;
974 goto out
; /* return; */
977 * if the checksum is true, then upstream wants us to calc
979 if (UAS_VALUE(ddp
->checksum
) &&
980 (UAS_VALUE(ddp
->checksum
) != ddp_checksum(mp
, 4))) {
981 dPrintf(D_M_DDP
, D_L_WARNING
,
982 ("Checksum error on incoming pkt, calc 0x%x, exp 0x%x",
983 ddp_checksum(mp
, 4), UAS_VALUE(ddp
->checksum
)));
984 snmpStats
.dd_checkSum
++;
985 at_ddp_stats
.rcv_bad_checksum
++;
987 goto out
; /* return; */
990 /*############### routing input checking */
992 /* Router mode special: we send "up-stack" packets for this node or coming from any
993 * other ports, but for the reserved atalk sockets (RTMP, ZIP, NBP [and EP])
994 * BTW, the way we know it's for the router and not the home port is that the
995 * MAC (ethernet) address is always the one of the interface we're on, but
996 * the AppleTalk address must be the one of the home port. If it's a multicast
997 * or another AppleTalk address, this is the router job's to figure out where it's
1000 /* *** a duplicate should be sent to any other client that is listening
1001 for packets of this type on a raw DDP socket *** */
1002 if (ddp_handler
[socket
].func
) {
1003 dPrintf(D_M_DDP
,D_L_INPUT
,
1004 ("ddp_input: skt %d hdnlr:0x%x\n",
1005 (u_int
) socket
, ddp_handler
[socket
].func
));
1007 snmpStats
.dd_inLocal
++;
1009 (*ddp_handler
[socket
].func
)(mp
, ifID
);
1010 goto out
; /* return; */
1012 dst_net
= NET_VALUE(ddp
->dst_net
);
1016 /* any node, wildcard or matching net */
1017 ((ddp
->dst_node
== 255) &&
1018 (((dst_net
>= ifID_home
->ifThisCableStart
) &&
1019 (dst_net
<= ifID_home
->ifThisCableEnd
)) ||
1021 /* this node is not online yet(?) */
1022 (ifID
->ifRoutingState
< PORT_ONLINE
)
1026 snmpStats
.dd_inLocal
++;
1028 if (ddp
->type
== DDP_ATP
) {
1029 if (atp_inputQ
[socket
] && (atp_inputQ
[socket
] != (gref_t
*)1)) {
1030 /* if there's an ATP pcb */
1032 goto out
; /* return; */
1034 } else if (ddp
->type
== DDP_ADSP
) {
1035 if (adsp_inputQ
[socket
]) {
1036 /* if there's an ADSP pcb */
1038 goto out
; /* return; */
1042 /* otherwise look for a DDP pcb;
1043 ATP / raw-DDP and ADSP / raw-DDP are possible */
1044 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
;
1045 gref
= gref
->atpcb_next
)
1046 if (gref
->lport
== socket
&&
1047 (gref
->ddptype
== 0 || gref
->ddptype
== ddp
->type
)) {
1048 dPrintf(D_M_DDP
, D_L_INPUT
,
1049 ("ddp_input: streamq, skt %d\n", socket
));
1050 if (gref
->atpcb_socket
) {
1051 struct sockaddr_at ddp_in
;
1052 ddp_in
.sat_len
= sizeof(ddp_in
);
1053 ddp_in
.sat_family
= AF_APPLETALK
;
1054 ddp_in
.sat_addr
.s_net
= NET_VALUE(ddp
->src_net
);
1055 ddp_in
.sat_addr
.s_node
= ddp
->src_node
;
1056 ddp_in
.sat_port
= ddp
->src_socket
;
1058 /* strip off DDP header if so indicated by
1060 if (gref
->ddp_flags
& DDPFLG_STRIPHDR
) {
1061 mp
= m_pullup((struct mbuf
*)mp
,
1064 gbuf_rinc(mp
, DDP_X_HDR_SIZE
);
1066 /* this should never happen because
1067 msgsize was checked earlier */
1068 at_ddp_stats
.rcv_bad_length
++;
1069 goto out
; /* return */
1073 if (sbappendaddr(&((gref
->atpcb_socket
)->so_rcv
),
1074 (struct sockaddr
*)&ddp_in
,
1078 sorwakeup(gref
->atpcb_socket
);
1080 atalk_putnext(gref
, mp
);
1082 goto out
; /* return */
1085 at_ddp_stats
.rcv_bad_socket
++;
1087 snmpStats
.dd_noHandler
++;
1088 dPrintf(D_M_DDP
, D_L_WARNING
,
1089 ("ddp_input: dropped pkt for socket %d\n", socket
));
1091 dPrintf(D_M_DDP
, D_L_ROUTING
,
1092 ("ddp_input: routing_needed from port=%d sock=%d\n",
1093 ifID
->ifPort
, ddp
->dst_socket
));
1095 snmpStats
.dd_fwdReq
++;
1096 if (((pktsIn
-pktsHome
+200) >= RouterMix
) && ((++pktsDropped
% 5) == 0)) {
1097 at_ddp_stats
.rcv_dropped_nobuf
++;
1101 routing_needed(mp
, ifID
, FALSE
);
1105 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1110 * ddp_router_output()
1113 * This is a modified version of ddp_output for router use.
1114 * The main difference is that the interface on which the packet needs
1115 * to be sent is specified and a *destination* AppleTalk address is passed
1116 * as an argument, this address may or may not be the same as the destination
1117 * address found in the ddp packet... This is the trick about routing, the
1118 * AppleTalk destination of the packet may not be the same as the Enet address
1119 * we send the packet too (ie, we may pass the baby to another router).
1122 int ddp_router_output(mp
, ifID
, addr_type
, router_net
, router_node
, enet_addr
)
1126 at_net_al router_net
;
1127 at_node router_node
;
1128 etalk_addr_t
*enet_addr
;
1130 register at_ddp_t
*ddp
;
1131 struct atalk_addr at_dest
;
1137 dPrintf(D_M_DDP
, D_L_WARNING
, ("BAD BAD ifID\n"));
1141 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
1143 if (ifID
->ifFlags
& AT_IFF_AURP
) { /* AURP link? */
1144 if (ddp_AURPsendx
) {
1145 fillin_pkt_chain(mp
);
1146 if (router_node
== 255)
1148 ddp_AURPsendx(AURPCODE_DATAPKT
, mp
, router_node
);
1156 /* keep some of the tests for now ####### */
1158 if (gbuf_msgsize(mp
) > DDP_DATAGRAM_SIZE
) {
1159 /* the packet is too large */
1160 dPrintf(D_M_DDP
, D_L_WARNING
,
1161 ("ddp_router_output: Packet too large size=%d\n",
1167 switch (addr_type
) {
1172 * Check for packet destined to the home stack
1175 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1176 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)) {
1177 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1178 ("ddp_r_output: sending back home from port=%d socket=%d\n",
1179 ifID
->ifPort
, ddp
->dst_socket
));
1181 UAS_ASSIGN(ddp
->checksum
, 0);
1182 ddp_input(mp
, ifID
);
1186 NET_ASSIGN(at_dest
.atalk_net
, router_net
);
1187 at_dest
.atalk_node
= router_node
;
1189 addr_flag
= AT_ADDR_NO_LOOP
;
1190 addr
= (char *)&at_dest
;
1191 dPrintf(D_M_DDP_LOW
, D_L_ROUTING_AT
,
1192 ("ddp_r_output: AT_ADDR out port=%d net %d:%d via rte %d:%d",
1193 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
, router_net
,
1198 addr_flag
= ET_ADDR
;
1199 addr
= (char *)enet_addr
;
1200 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1201 ("ddp_r_output: ET_ADDR out port=%d net %d:%d\n",
1202 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
));
1206 if (ifID
->ifState
== LAP_OFFLINE
) {
1211 fillin_pkt_chain(mp
);
1214 struct etalk_addr dest_addr
;
1215 struct atalk_addr dest_at_addr
;
1216 int loop
= TRUE
; /* flag to aarp to loopback (default) */
1220 /* the incoming frame is of the form {flag, address, ddp...}
1221 * where "flag" indicates whether the address is an 802.3
1222 * (link) address, or an appletalk address. If it's an
1223 * 802.3 address, the packet can just go out to the network
1224 * through PAT, if it's an appletalk address, AT->802.3 address
1225 * resolution needs to be done.
1226 * If 802.3 address is known, strip off the flag and 802.3
1227 * address, and prepend 802.2 and 802.3 headers.
1231 addr_flag
= *(u_char
*)gbuf_rptr(m
);
1235 switch (addr_flag
) {
1236 case AT_ADDR_NO_LOOP
:
1241 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
1242 gbuf_rinc(m
,sizeof(struct atalk_addr
));
1244 dest_at_addr
= *(struct atalk_addr
*)addr
;
1248 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
1249 gbuf_rinc(m
,sizeof(struct etalk_addr
));
1251 dest_addr
= *(struct etalk_addr
*)addr
;
1254 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
1255 ("ddp_router_output: Unknown addr_flag = 0x%x\n", addr_flag
));
1257 gbuf_freel(m
); /* unknown address type, chuck it */
1263 /* At this point, rptr points to ddp header for sure */
1264 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
1265 /* see if this is a ZIP packet that we need
1266 * to let through even though network is
1269 if (zip_type_packet(m
) == 0) {
1275 ifID
->stats
.xmit_packets
++;
1276 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
1277 snmpStats
.dd_outLong
++;
1279 switch (addr_flag
) {
1280 case AT_ADDR_NO_LOOP
:
1283 * we don't want elap to be looking into ddp header, so
1284 * it doesn't know net#, consequently can't do
1285 * AMT_LOOKUP. That task left to aarp now.
1287 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
1290 pat_output(ifID
, m
, &dest_addr
, 0);
1296 } /* ddp_router_output */
1298 /*****************************************/
1300 void rt_delete(NetStop
, NetStart
)
1301 unsigned short NetStop
;
1302 unsigned short NetStart
;
1307 ATDISABLE(s
, ddpinp_lock
);
1308 if ((found
= rt_bdelete(NetStop
, NetStart
)) != 0) {
1309 bzero(found
, sizeof(RT_entry
));
1310 found
->right
= RT_table_freelist
;
1311 RT_table_freelist
= found
;
1313 ATENABLE(s
, ddpinp_lock
);
1316 int ddp_AURPfuncx(code
, param
, node
)
1321 extern void rtmp_timeout();
1322 extern void rtmp_send_port();
1327 case AURPCODE_DATAPKT
: /* data packet */
1329 dPrintf(D_M_DDP
, D_L_TRACE
, ("ddp_AURPfuncx: data, 0x%x, %d\n",
1330 (u_int
) aurp_ifID
, node
));
1332 ddp_input((gbuf_t
*)param
, aurp_ifID
);
1334 gbuf_freem((gbuf_t
*)param
);
1337 case AURPCODE_REG
: /* register/deregister */
1340 ddp_AURPsendx
= (void(*)())param
;
1343 /* register AURP callback function */
1346 for (k
=(IFID_HOME
+1); k
< IF_TOTAL_MAX
; k
++) {
1347 if (ifID_table
[k
] == 0) {
1348 aurp_ifID
= &at_interfaces
[k
];
1349 aurp_ifID
->ifFlags
= RTR_XNET_PORT
;
1350 ddp_add_if(aurp_ifID
);
1351 aurp_ifID
->ifState
= LAP_ONLINE
;
1352 aurp_ifID
->ifRoutingState
= PORT_ONLINE
;
1353 dPrintf(D_M_DDP
, D_L_TRACE
,
1354 ("ddp_AURPfuncx: on, 0x%x\n",
1355 (u_int
) aurp_ifID
));
1357 ddp_AURPsendx(AURPCODE_DEBUGINFO
,
1358 &dbgBits
, aurp_ifID
->ifPort
);
1365 /* deregister AURP callback function */
1367 rtmp_purge(aurp_ifID
);
1368 ddp_rem_if(aurp_ifID
);
1369 aurp_ifID
->ifState
= LAP_OFFLINE
;
1370 aurp_ifID
->ifRoutingState
= PORT_OFFLINE
;
1371 dPrintf(D_M_DDP
, D_L_TRACE
,
1372 ("ddp_AURPfuncx: off, 0x%x\n", (u_int
) aurp_ifID
));
1378 case AURPCODE_AURPPROTO
: /* proto type - AURP */
1380 aurp_ifID
->ifFlags
|= AT_IFF_AURP
;
1389 /* checks to see if address of packet is for one of our interfaces
1390 returns *ifID if it's for us, NULL if not
1392 at_ifaddr_t
*forUs(ddp
)
1393 register at_ddp_t
*ddp
;
1397 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
1398 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1399 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)
1401 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1402 ("pkt was for port %d\n", ifID
->ifPort
));
1408 return((at_ifaddr_t
*)NULL
);