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
;
93 extern int *adsp_pidM
;
94 extern struct atpcb
*atp_inputQ
[];
95 extern CCB
*adsp_inputQ
[];
97 at_ifaddr_t
*forUs(at_ddp_t
*);
99 void ddp_input(), ddp_notify_nbp();
101 extern void routing_needed();
102 extern void ddp_brt_sweep();
108 void init_ddp_handler()
110 bzero(ddp_handler
, sizeof(ddp_handler
));
113 void add_ddp_handler(ddp_socket
, input_func
)
115 void (*input_func
)();
117 ddp_handler
[ddp_socket
].func
= input_func
;
127 * Raw DDP socket option processing.
129 int ddp_ctloutput(so
, sopt
)
131 struct sockopt
*sopt
;
133 struct atpcb
*at_pcb
= sotoatpcb(so
);
134 int optval
, error
= 0;
136 if (sopt
->sopt_level
!= ATPROTO_NONE
)
139 switch (sopt
->sopt_dir
) {
142 switch (sopt
->sopt_name
) {
144 optval
= at_pcb
->ddp_flags
& DDPFLG_HDRINCL
;
145 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
148 optval
= at_pcb
->ddp_flags
& DDPFLG_CHKSUM
;
149 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
152 optval
= at_pcb
->ddp_flags
& DDPFLG_STRIPHDR
;
153 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
156 optval
= at_pcb
->ddp_flags
& DDPFLG_SLFSND
;
157 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
159 case DDP_GETSOCKNAME
:
162 addr
.inet
.net
= at_pcb
->laddr
.s_net
;
163 addr
.inet
.node
= at_pcb
->laddr
.s_node
;
164 addr
.inet
.socket
= at_pcb
->lport
;
165 addr
.ddptype
= at_pcb
->ddptype
;
166 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
175 switch (sopt
->sopt_name
) {
177 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
182 at_pcb
->ddp_flags
|= DDPFLG_HDRINCL
;
184 at_pcb
->ddp_flags
&= ~DDPFLG_HDRINCL
;
187 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
192 at_pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
194 at_pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
197 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
202 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
204 at_pcb
->ddp_flags
&= ~DDPFLG_STRIPHDR
;
207 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
212 at_pcb
->ddp_flags
|= DDPFLG_SLFSND
;
214 at_pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
226 /****************************************************************/
229 /* Support Routines */
232 /****************************************************************/
239 * This procedure determines the checksum of an extended DDP datagram.
240 * Add the unsigned bytes into an unsigned 16-bit accumulator.
241 * After each add, rotate the sign bit into the low order bit of
242 * the accumulator. When done, if the checksum is 0, changed into 0xFFFF.
245 * checksum = ddp_checksum(mp, offset)
248 * mp pointer to the datagram gbuf_t
249 * offset offset to start at in first gbuf_t block
256 u_short
ddp_checksum(mp
, offset
)
260 register u_char
*data
;
262 register u_short checksum
;
267 if (offset
>= gbuf_len(mp
))
268 offset
-= gbuf_len(mp
);
270 data
= ((unsigned char *) gbuf_rptr(mp
)) + offset
;
271 length
= gbuf_len(mp
) - offset
;
273 /* Portable checksum from 3.0 */
276 checksum
= (checksum
& 0x8000) ?
277 ((checksum
<< 1) | 1) : (checksum
<< 1);
280 } while ( (mp
= gbuf_cont(mp
)) );
292 * This procedure is called by each LAP interface when it wants to place
293 * itself online. The LAP interfaces passes in a pointer to its at_if
294 * struct, which is added to DDP's list of active interfaces (at_ifQueueHd).
295 * When DDP wants to transmit a packet, it searches this list for the
298 * If AT_IFF_DEFAULT is set, then this interface is to be brought online
299 * as the interface DDP socket addresses are tied to. Of course there can
300 * be only one default interface; we return an error if it's already set.
303 * ret_status = ddp_add_if(ifID)
306 * ifID pointer to LAP interface's at_if struct.
309 * 0 Procedure successfully completed.
310 * EALREADY This interface is already online, or there is
311 * already a default interface.
312 * ENOBUFS Cannot allocate input queue
316 register at_ifaddr_t
*ifID
;
320 dPrintf(D_M_DDP
, D_L_STARTUP
,
321 ("ddp_add_if: called, ifID:0x%x\n", (u_int
) ifID
));
323 if (ifID
->ifFlags
& AT_IFF_DEFAULT
) {
325 return(EEXIST
); /* home port already set */
331 for (port
=IFID_HOME
+1; port
<IF_TOTAL_MAX
; port
++)
332 if (!ifID_table
[port
]) {
335 if (port
== IF_TOTAL_MAX
) /* no space left */
339 /* allocate an et_aarp_amt structure */
340 if ((aarp_table
[port
] =
341 (aarp_amt_array
*)_MALLOC(sizeof(aarp_amt_array
),
342 M_RTABLE
, M_WAITOK
)) == NULL
)
345 dPrintf(D_M_DDP
, D_L_STARTUP
, ("ddp:adding ifID_table[%d]\n", port
));
347 /* add i/f to port list */
348 ifID_table
[port
] = ifID
;
349 ifID
->ifPort
= port
; /* set ddp port # in ifID */
351 /* Add this interface to the list of online interfaces */
352 TAILQ_INSERT_TAIL(&at_ifQueueHd
, ifID
, aa_link
);
361 * This procedure is called by each LAP interface when it wants to take
362 * itself offline. The LAP interfaces passes in a pointer to its at_if
363 * struct; DDP's list of active interfaces (at_ifQueueHd) is searched and
364 * this interface is removed from the list. DDP can still transmit
365 * packets as long as this interface is not the default interface; the
366 * sender will just get ENETUNREACH errors when it tries to send to an
367 * interface that went offline. However, if the default interface is
368 * taken offline, we no longer have a node ID to use as a source address
369 * and DDP must return ENETDOWN when a caller tries to send a packet.
372 * ifID pointer to LAP interface's at_if struct.
375 void ddp_rem_if(ifID
)
376 register at_ifaddr_t
*ifID
;
378 struct ifaddr
*ifa
= &ifID
->aa_ifa
;
380 /* un-do processing done in SIOCSIFADDR */
382 ifnet_lock_exclusive(ifID
->aa_ifp
);
383 if_detach_ifa(ifID
->aa_ifp
, ifa
);
384 ifa
->ifa_addr
= NULL
;
385 ifnet_lock_done(ifID
->aa_ifp
);
387 if (ifID
->at_dl_tag
) {
388 /* dlil_detach_protocol(ifID->at_dl_tag); */
389 ether_detach_at(ifID
->aa_ifp
);
393 /* un-do processing done in ddp_add_if() */
395 if (aarp_table
[ifID
->ifPort
]) {
396 FREE(aarp_table
[ifID
->ifPort
], M_RTABLE
);
397 aarp_table
[ifID
->ifPort
] = NULL
;
400 at_state
.flags
|= AT_ST_IF_CHANGED
;
403 trackrouter_rem_if(ifID
);
404 TAILQ_REMOVE(&at_ifQueueHd
, ifID
, aa_link
);
405 ifID_table
[ifID
->ifPort
] = NULL
;
406 ifID
->ifName
[0] = '\0';
410 /* *** deallocate ifID, eventually *** */
414 * The user may have registered an NVE with the NBP on a socket. When the
415 * socket is closed, the NVE should be deleted from NBP's name table. The
416 * user should delete the NVE before the socket is shut down, but there
417 * may be circumstances when he can't. So, whenever a DDP socket is closed,
418 * this routine is used to notify NBP of the socket closure. This would
419 * help NBP get rid of all NVE's registered on the socket.
422 /* *** Do we still need to do this? *** */
423 int ot_ddp_check_socket(socket
, pid
)
424 unsigned char socket
;
430 dPrintf(D_M_DDP
, D_L_INFO
, ("ot_ddp_check_socket: %d\n", socket
));
431 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
; gref
= gref
->atpcb_next
)
432 if (gref
->lport
== socket
&& gref
->pid
== pid
)
434 if ((atp_inputQ
[socket
] != NULL
) && (atp_inputQ
[socket
] != (gref_t
*)1)
435 && (atp_pidM
[socket
] == pid
))
437 if ((adsp_inputQ
[socket
] != NULL
) && (adsp_pidM
[socket
] == pid
))
443 void ddp_notify_nbp(socket
, pid
, ddptype
)
444 unsigned char socket
;
446 unsigned char ddptype
; /* not used */
449 nve_entry_t
*nve_entry
, *nve_next
;
451 if (at_state
.flags
& AT_ST_STARTED
) {
452 /* *** NBP_CLOSE_NOTE processing (from ddp_nbp.c) *** */
453 for ((nve_entry
= TAILQ_FIRST(&name_registry
)); nve_entry
; nve_entry
= nve_next
) {
454 nve_next
= TAILQ_NEXT(nve_entry
, nve_link
);
455 if ((at_socket
)socket
== nve_entry
->address
.socket
&&
456 /* *** check complete address and ddptype here *** */
457 pid
== nve_entry
->pid
&&
458 ot_ddp_check_socket(nve_entry
->address
.socket
,
459 nve_entry
->pid
) < 2) {
460 /* NB: nbp_delete_entry calls TAILQ_REMOVE */
461 nbp_delete_entry(nve_entry
);
465 } /* ddp_notify_nbp */
467 static void fillin_pkt_chain(m
)
472 *ddp
= (at_ddp_t
*)gbuf_rptr(m
),
476 if (UAS_VALUE(ddp
->checksum
)) {
477 tmp
= ddp_checksum(m
, 4);
478 UAS_ASSIGN_HTON(ddp
->checksum
, tmp
);
481 for (tmp_m
=gbuf_next(tmp_m
); tmp_m
; tmp_m
=gbuf_next(tmp_m
)) {
482 tmp_ddp
= (at_ddp_t
*)gbuf_rptr(tmp_m
);
483 DDPLEN_ASSIGN(tmp_ddp
, gbuf_msgsize(tmp_m
));
486 NET_NET(tmp_ddp
->src_net
, ddp
->src_net
);
487 tmp_ddp
->src_node
= ddp
->src_node
;
488 tmp_ddp
->src_socket
= ddp
->src_socket
;
489 if (UAS_VALUE(tmp_ddp
->checksum
)) {
490 tmp
= ddp_checksum(tmp_m
, 4);
491 UAS_ASSIGN_HTON(ddp
->checksum
, tmp
);
496 /* There are various ways a packet may go out.... it may be sent out
497 * directly to destination node, or sent to a random router or sent
498 * to a router whose entry exists in Best Router Cache. Following are
499 * constants used WITHIN this routine to keep track of choice of destination
501 #define DIRECT_ADDR 1
503 #define BRIDGE_ADDR 3
509 * Called to queue a atp/ddp data packet on the network interface.
510 * It returns 0 normally, and an errno in case of error.
511 * The mbuf chain pointed to by *mp is consumed on success, and
512 * freed in case of error.
515 int ddp_output(mp
, src_socket
, src_addr_included
)
516 register gbuf_t
**mp
;
517 at_socket src_socket
;
518 int src_addr_included
;
520 register at_ifaddr_t
*ifID
= ifID_home
, *ifIDTmp
= NULL
;
521 register at_ddp_t
*ddp
;
522 register ddp_brt_t
*brt
;
523 register at_net_al dst_net
;
525 struct atalk_addr at_dest
;
526 at_ifaddr_t
*ARouterIf
= NULL
;
534 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_START
, 0,
537 snmpStats
.dd_outReq
++;
540 ddp
= (at_ddp_t
*)gbuf_rptr(m
);
543 /* Device/Interface not configured */
544 dPrintf(D_M_DDP
, D_L_ERROR
, ("Device/Interface not configured"));
547 goto exit_ddp_output
;
550 if ((ddp
->dst_socket
> (unsigned) (DDP_SOCKET_LAST
+ 1)) ||
551 (ddp
->dst_socket
< DDP_SOCKET_1st_RESERVED
)) {
552 dPrintf(D_M_DDP
, D_L_ERROR
,
553 ("Illegal destination socket on outgoing packet (0x%x)",
555 at_ddp_stats
.xmit_bad_addr
++;
558 goto exit_ddp_output
;
560 if ((len
= gbuf_msgsize(*mp
)) > DDP_DATAGRAM_SIZE
) {
561 /* the packet is too large */
562 dPrintf(D_M_DDP
, D_L_ERROR
,
563 ("Outgoing packet too long (len=%d bytes)", len
));
564 at_ddp_stats
.xmit_bad_length
++;
567 goto exit_ddp_output
;
569 at_ddp_stats
.xmit_bytes
+= len
;
570 at_ddp_stats
.xmit_packets
++;
572 DDPLEN_ASSIGN(ddp
, len
);
576 /* If this packet is for the same node, loop it back
577 * up... Note that for LocalTalk, dst_net zero means "THIS_NET", so
578 * address 0.nn is eligible for loopback. For Extended EtherTalk,
579 * dst_net 0 can be used only for cable-wide or zone-wide
580 * broadcasts (0.ff) and as such, address of the form 0.nn is NOT
581 * eligible for loopback.
583 dst_net
= NET_VALUE(ddp
->dst_net
);
585 /* If our packet is destined for the 'virtual' bridge
586 * address of NODE==0xFE, replace that address with a
587 * real bridge address.
589 if ((ddp
->dst_node
== 0xfe) &&
590 ((dst_net
== ATADDR_ANYNET
) ||
591 (dst_net
>= ifID_home
->ifThisCableStart
&&
592 dst_net
<= ifID_home
->ifThisCableEnd
))) {
593 /* if there's a router that's not us, it's in ifID_home */
594 NET_ASSIGN(ddp
->dst_net
, ifID_home
->ifARouter
.s_net
);
595 dst_net
= ifID_home
->ifARouter
.s_net
;
596 ddp
->dst_node
= ifID_home
->ifARouter
.s_node
;
599 if (MULTIHOME_MODE
&& (ifIDTmp
= forUs(ddp
))) {
602 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
603 ("ddp_out: for us if:%s\n", ifIDTmp
->ifName
));
607 loop
= ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
608 (dst_net
== ifID
->ifThisNode
.s_net
)
611 gbuf_t
*mdata
, *mdata_next
;
613 if (!MULTIHOME_MODE
|| !src_addr_included
) {
614 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
615 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
617 ddp
->src_socket
= src_socket
;
619 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
620 ("ddp_output: loop to %d:%d port=%d\n",
621 NET_VALUE(ddp
->dst_net
),
625 fillin_pkt_chain(*mp
);
627 dPrintf(D_M_DDP
, D_L_VERBOSE
,
628 ("Looping back packet from skt 0x%x to skt 0x%x\n",
629 ddp
->src_socket
, ddp
->dst_socket
));
631 for (mdata
= *mp
; mdata
; mdata
= mdata_next
) {
632 mdata_next
= gbuf_next(mdata
);
633 gbuf_next(mdata
) = 0;
634 ddp_input(mdata
, ifID
);
636 goto exit_ddp_output
;
638 if ((ddp
->dst_socket
== ZIP_SOCKET
) &&
639 (zip_type_packet(*mp
) == ZIP_GETMYZONE
)) {
640 ddp
->src_socket
= src_socket
;
641 error
= zip_handle_getmyzone(ifID
, *mp
);
643 goto exit_ddp_output
;
646 * find out the interface on which the packet should go out
648 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
649 if ((ifID
->ifThisNode
.s_net
== dst_net
) || (dst_net
== 0))
650 /* the message is either going out (i) on the same
651 * NETWORK in case of LocalTalk, or (ii) on the same
652 * CABLE in case of Extended AppleTalk (EtherTalk).
656 if ((ifID
->ifThisCableStart
<= dst_net
) &&
657 (ifID
->ifThisCableEnd
>= dst_net
)
659 /* We're on EtherTalk and the message is going out to
660 * some other network on the same cable.
664 if (ARouterIf
== NULL
&& ATALK_VALUE(ifID
->ifARouter
))
667 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
668 ("ddp_output: after search ifid:0x%x %s ifID_home:0x%x\n",
669 (u_int
)ifID
, ifID
? ifID
->ifName
: "",
673 /* located the interface where the packet should
674 * go.... the "first-hop" destination address
675 * must be the same as real destination address.
677 addr_type
= DIRECT_ADDR
;
679 /* no, the destination network number does
680 * not match known network numbers. If we have
681 * heard from this network recently, BRT table
682 * may have address of a router we could use!
684 if (!MULTIPORT_MODE
) {
685 BRT_LOOK (brt
, dst_net
);
687 /* Bingo... BRT has an entry for this network.
688 * Use the link address as is.
690 dPrintf(D_M_DDP
, D_L_VERBOSE
,
691 ("Found BRT entry to send to net 0x%x", dst_net
));
692 at_ddp_stats
.xmit_BRT_used
++;
693 addr_type
= BRT_ENTRY
;
696 /* No BRT entry available for dest network... do we
697 * know of any router at all??
699 if ((ifID
= ARouterIf
) != NULL
)
700 addr_type
= BRIDGE_ADDR
;
702 dPrintf(D_M_DDP
, D_L_WARNING
,
703 ("Found no interface to send pkt"));
704 at_ddp_stats
.xmit_bad_addr
++;
707 goto exit_ddp_output
;
711 else { /* We are in multiport mode, so we can bypass all the rest
712 * and directly ask for the routing of the packet
714 at_ddp_stats
.xmit_BRT_used
++;
717 if (!src_addr_included
) {
718 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
719 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
721 ddp
->src_socket
= src_socket
;
722 routing_needed(*mp
, ifID
, TRUE
);
724 goto exit_ddp_output
;
727 /* by the time we land here, we know the interface on
728 * which this packet is going out.... ifID.
730 if (ifID
->ifState
== LAP_OFFLINE
) {
732 goto exit_ddp_output
;
738 at_dest.atalk_unused = 0;
740 NET_ASSIGN(at_dest
.atalk_net
, dst_net
);
741 at_dest
.atalk_node
= ddp
->dst_node
;
743 addr
= (char *)&at_dest
;
747 addr
= (char *)&brt
->et_addr
;
750 NET_ASSIGN(at_dest
.atalk_net
, ifID
->ifARouter
.s_net
);
751 at_dest
.atalk_node
= ifID
->ifARouter
.s_node
;
753 addr
= (char *)&at_dest
;
757 /* Irrespective of the interface on which
758 * the packet is going out, we always put the
759 * same source address on the packet (unless multihoming mode).
761 if (MULTIHOME_MODE
) {
762 if (!src_addr_included
) {
763 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
764 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
768 ddp
->src_node
= ifID_home
->ifThisNode
.s_node
;
769 NET_ASSIGN(ddp
->src_net
, ifID_home
->ifThisNode
.s_net
);
771 ddp
->src_socket
= src_socket
;
773 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
774 ("ddp_output: going out to %d:%d skt%d on %s\n",
775 dst_net
, ddp
->dst_node
, ddp
->dst_socket
, ifID
->ifName
));
777 fillin_pkt_chain(*mp
);
780 struct etalk_addr dest_addr
;
781 struct atalk_addr dest_at_addr
;
782 int loop
= TRUE
; /* flag to aarp to loopback (default) */
786 /* the incoming frame is of the form {flag, address, ddp...}
787 * where "flag" indicates whether the address is an 802.3
788 * (link) address, or an appletalk address. If it's an
789 * 802.3 address, the packet can just go out to the network
790 * through PAT, if it's an appletalk address, AT->802.3 address
791 * resolution needs to be done.
792 * If 802.3 address is known, strip off the flag and 802.3
793 * address, and prepend 802.2 and 802.3 headers.
797 addr_flag
= *(u_char
*)gbuf_rptr(m
);
802 case AT_ADDR_NO_LOOP
:
807 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
808 gbuf_rinc(m
,sizeof(struct atalk_addr
));
810 dest_at_addr
= *(struct atalk_addr
*)addr
;
814 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
815 gbuf_rinc(m
,sizeof(struct etalk_addr
));
817 dest_addr
= *(struct etalk_addr
*)addr
;
820 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
821 ("ddp_output: Unknown addr_flag = 0x%x\n", addr_flag
));
822 gbuf_freel(m
); /* unknown address type, chuck it */
823 goto exit_ddp_output
;
828 /* At this point, rptr points to ddp header for sure */
829 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
830 /* see if this is a ZIP packet that we need
831 * to let through even though network is
834 if (zip_type_packet(m
) == 0) {
836 goto exit_ddp_output
;
840 ifID
->stats
.xmit_packets
++;
841 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
842 snmpStats
.dd_outLong
++;
845 case AT_ADDR_NO_LOOP
:
848 * we don't want elap to be looking into ddp header, so
849 * it doesn't know net#, consequently can't do
850 * AMT_LOOKUP. That task left to aarp now.
852 aarp_send_data(m
,ifID
, &dest_at_addr
, loop
);
855 pat_output(ifID
, m
, &dest_addr
, 0);
860 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_END
, 0,
865 void ddp_input(mp
, ifID
)
867 register at_ifaddr_t
*ifID
;
869 register at_ddp_t
*ddp
; /* DDP header */
870 register int msgsize
;
871 register at_socket socket
;
873 register at_net_al dst_net
;
875 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_START
, 0,
876 ifID
, mp
, gbuf_len(mp
),0);
878 /* Makes sure we know the default interface before starting to
879 * accept incomming packets. If we don't we may end up with a
880 * null ifID_table[0] and have impredicable results (specially
881 * in router mode. This is a transitory state (because we can
882 * begin to receive packet while we're not completly set up yet.
885 if (ifID_home
== (at_ifaddr_t
*)NULL
) {
886 dPrintf(D_M_DDP
, D_L_ERROR
,
887 ("dropped incoming packet ifID_home not set yet\n"));
889 goto out
; /* return */
893 * if a DDP packet has been broadcast, we're going to get a copy of
894 * it here; if it originated at user level via a write on a DDP
895 * socket; when it gets here, the first block in the chain will be
896 * empty since it only contained the lap level header which will be
897 * stripped in the lap level immediately below ddp
900 if ((mp
= (gbuf_t
*)ddp_compress_msg(mp
)) == NULL
) {
901 dPrintf(D_M_DDP
, D_L_ERROR
,
902 ("dropped short incoming ET packet (len %d)", 0));
903 snmpStats
.dd_inTotal
++;
904 at_ddp_stats
.rcv_bad_length
++;
905 goto out
; /* return; */
907 msgsize
= gbuf_msgsize(mp
);
909 at_ddp_stats
.rcv_bytes
+= msgsize
;
910 at_ddp_stats
.rcv_packets
++;
912 /* if the interface pointer is 0, the packet has been
913 * looped back by 'write' half of DDP. It is of the
914 * form {extended ddp,...}. The packet is meant to go
915 * up to some socket on the same node.
917 if (!ifID
) /* if loop back is specified */
918 ifID
= ifID_home
; /* that means the home port */
920 /* the incoming datagram has extended DDP header and is of
921 * the form {ddp,...}.
923 if (msgsize
< DDP_X_HDR_SIZE
) {
924 dPrintf(D_M_DDP
, D_L_ERROR
,
925 ("dropped short incoming ET packet (len %d)", msgsize
));
926 at_ddp_stats
.rcv_bad_length
++;
928 goto out
; /* return; */
931 * At this point, the message is always of the form
932 * {extended ddp, ... }.
934 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
935 len
= DDPLEN_VALUE(ddp
);
937 if (msgsize
!= len
) {
938 if ((unsigned) msgsize
> len
) {
939 if (len
< DDP_X_HDR_SIZE
) {
940 dPrintf(D_M_DDP
, D_L_ERROR
,
941 ("Length problems, ddp length %d, buffer length %d",
943 snmpStats
.dd_tooLong
++;
944 at_ddp_stats
.rcv_bad_length
++;
946 goto out
; /* return; */
949 * shave off the extra bytes from the end of message
951 mp
= ddp_adjmsg(mp
, -(msgsize
- len
)) ? mp
: 0;
953 goto out
; /* return; */
955 dPrintf(D_M_DDP
, D_L_ERROR
,
956 ("Length problems, ddp length %d, buffer length %d",
958 snmpStats
.dd_tooShort
++;
959 at_ddp_stats
.rcv_bad_length
++;
961 goto out
; /* return; */
964 socket
= ddp
->dst_socket
;
967 * We want everything in router mode, specially socket 254 for nbp so we need
968 * to bypass this test when we are a router.
971 if (!MULTIPORT_MODE
&& (socket
> DDP_SOCKET_LAST
||
972 socket
< DDP_SOCKET_1st_RESERVED
)) {
973 dPrintf(D_M_DDP
, D_L_WARNING
,
974 ("Bad dst socket on incoming packet (0x%x)",
976 at_ddp_stats
.rcv_bad_socket
++;
978 goto out
; /* return; */
981 * if the checksum is true, then upstream wants us to calc
983 if (UAS_VALUE(ddp
->checksum
) &&
984 (UAS_VALUE_NTOH(ddp
->checksum
) != ddp_checksum(mp
, 4))) {
985 dPrintf(D_M_DDP
, D_L_WARNING
,
986 ("Checksum error on incoming pkt, calc 0x%x, exp 0x%x",
987 ddp_checksum(mp
, 4), UAS_VALUE_NTOH(ddp
->checksum
)));
988 snmpStats
.dd_checkSum
++;
989 at_ddp_stats
.rcv_bad_checksum
++;
991 goto out
; /* return; */
994 /*############### routing input checking */
996 /* Router mode special: we send "up-stack" packets for this node or coming from any
997 * other ports, but for the reserved atalk sockets (RTMP, ZIP, NBP [and EP])
998 * BTW, the way we know it's for the router and not the home port is that the
999 * MAC (ethernet) address is always the one of the interface we're on, but
1000 * the AppleTalk address must be the one of the home port. If it's a multicast
1001 * or another AppleTalk address, this is the router job's to figure out where it's
1004 /* *** a duplicate should be sent to any other client that is listening
1005 for packets of this type on a raw DDP socket *** */
1006 if (ddp_handler
[socket
].func
) {
1007 dPrintf(D_M_DDP
,D_L_INPUT
,
1008 ("ddp_input: skt %d hdnlr:0x%x\n",
1009 (u_int
) socket
, ddp_handler
[socket
].func
));
1011 snmpStats
.dd_inLocal
++;
1013 (*ddp_handler
[socket
].func
)(mp
, ifID
);
1014 goto out
; /* return; */
1016 dst_net
= NET_VALUE(ddp
->dst_net
);
1020 /* any node, wildcard or matching net */
1021 ((ddp
->dst_node
== 255) &&
1022 (((dst_net
>= ifID_home
->ifThisCableStart
) &&
1023 (dst_net
<= ifID_home
->ifThisCableEnd
)) ||
1025 /* this node is not online yet(?) */
1026 (ifID
->ifRoutingState
< PORT_ONLINE
)
1030 snmpStats
.dd_inLocal
++;
1032 if (ddp
->type
== DDP_ATP
) {
1033 if (atp_inputQ
[socket
] && (atp_inputQ
[socket
] != (gref_t
*)1)) {
1034 /* if there's an ATP pcb */
1036 goto out
; /* return; */
1038 } else if (ddp
->type
== DDP_ADSP
) {
1039 if (adsp_inputQ
[socket
]) {
1040 /* if there's an ADSP pcb */
1042 goto out
; /* return; */
1046 /* otherwise look for a DDP pcb;
1047 ATP / raw-DDP and ADSP / raw-DDP are possible */
1048 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
;
1049 gref
= gref
->atpcb_next
)
1050 if (gref
->lport
== socket
&&
1051 (gref
->ddptype
== 0 || gref
->ddptype
== ddp
->type
)) {
1052 dPrintf(D_M_DDP
, D_L_INPUT
,
1053 ("ddp_input: streamq, skt %d\n", socket
));
1054 if (gref
->atpcb_socket
) {
1055 struct sockaddr_at ddp_in
;
1056 ddp_in
.sat_len
= sizeof(ddp_in
);
1057 ddp_in
.sat_family
= AF_APPLETALK
;
1058 ddp_in
.sat_addr
.s_net
= NET_VALUE(ddp
->src_net
);
1059 ddp_in
.sat_addr
.s_node
= ddp
->src_node
;
1060 ddp_in
.sat_port
= ddp
->src_socket
;
1062 /* strip off DDP header if so indicated by
1064 if (gref
->ddp_flags
& DDPFLG_STRIPHDR
) {
1065 mp
= m_pullup((struct mbuf
*)mp
,
1068 gbuf_rinc(mp
, DDP_X_HDR_SIZE
);
1070 /* this should never happen because
1071 msgsize was checked earlier */
1072 at_ddp_stats
.rcv_bad_length
++;
1073 goto out
; /* return */
1077 if (sbappendaddr(&((gref
->atpcb_socket
)->so_rcv
),
1078 (struct sockaddr
*)&ddp_in
,
1079 mp
, 0, NULL
) != 0) {
1080 sorwakeup(gref
->atpcb_socket
);
1083 atalk_putnext(gref
, mp
);
1085 goto out
; /* return */
1088 at_ddp_stats
.rcv_bad_socket
++;
1090 snmpStats
.dd_noHandler
++;
1091 dPrintf(D_M_DDP
, D_L_WARNING
,
1092 ("ddp_input: dropped pkt for socket %d\n", socket
));
1094 dPrintf(D_M_DDP
, D_L_ROUTING
,
1095 ("ddp_input: routing_needed from port=%d sock=%d\n",
1096 ifID
->ifPort
, ddp
->dst_socket
));
1098 snmpStats
.dd_fwdReq
++;
1099 if (((pktsIn
-pktsHome
+200) >= RouterMix
) && ((++pktsDropped
% 5) == 0)) {
1100 at_ddp_stats
.rcv_dropped_nobuf
++;
1104 routing_needed(mp
, ifID
, FALSE
);
1108 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1113 * ddp_router_output()
1116 * This is a modified version of ddp_output for router use.
1117 * The main difference is that the interface on which the packet needs
1118 * to be sent is specified and a *destination* AppleTalk address is passed
1119 * as an argument, this address may or may not be the same as the destination
1120 * address found in the ddp packet... This is the trick about routing, the
1121 * AppleTalk destination of the packet may not be the same as the Enet address
1122 * we send the packet too (ie, we may pass the baby to another router).
1125 int ddp_router_output(mp
, ifID
, addr_type
, router_net
, router_node
, enet_addr
)
1129 at_net_al router_net
;
1130 at_node router_node
;
1131 etalk_addr_t
*enet_addr
;
1133 register at_ddp_t
*ddp
;
1134 struct atalk_addr at_dest
;
1140 dPrintf(D_M_DDP
, D_L_WARNING
, ("BAD BAD ifID\n"));
1144 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
1147 if (ifID
->ifFlags
& AT_IFF_AURP
) { /* AURP link? */
1148 if (ddp_AURPsendx
) {
1149 fillin_pkt_chain(mp
);
1150 if (router_node
== 255)
1152 ddp_AURPsendx(AURPCODE_DATAPKT
, mp
, router_node
);
1161 /* keep some of the tests for now ####### */
1163 if (gbuf_msgsize(mp
) > DDP_DATAGRAM_SIZE
) {
1164 /* the packet is too large */
1165 dPrintf(D_M_DDP
, D_L_WARNING
,
1166 ("ddp_router_output: Packet too large size=%d\n",
1172 switch (addr_type
) {
1177 * Check for packet destined to the home stack
1180 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1181 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)) {
1182 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1183 ("ddp_r_output: sending back home from port=%d socket=%d\n",
1184 ifID
->ifPort
, ddp
->dst_socket
));
1186 UAS_ASSIGN(ddp
->checksum
, 0);
1187 ddp_input(mp
, ifID
);
1191 NET_ASSIGN(at_dest
.atalk_net
, router_net
);
1192 at_dest
.atalk_node
= router_node
;
1194 addr_flag
= AT_ADDR_NO_LOOP
;
1195 addr
= (char *)&at_dest
;
1196 dPrintf(D_M_DDP_LOW
, D_L_ROUTING_AT
,
1197 ("ddp_r_output: AT_ADDR out port=%d net %d:%d via rte %d:%d",
1198 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
, router_net
,
1203 addr_flag
= ET_ADDR
;
1204 addr
= (char *)enet_addr
;
1205 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1206 ("ddp_r_output: ET_ADDR out port=%d net %d:%d\n",
1207 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
));
1211 if (ifID
->ifState
== LAP_OFFLINE
) {
1216 fillin_pkt_chain(mp
);
1219 struct etalk_addr dest_addr
;
1220 struct atalk_addr dest_at_addr
;
1221 int loop
= TRUE
; /* flag to aarp to loopback (default) */
1225 /* the incoming frame is of the form {flag, address, ddp...}
1226 * where "flag" indicates whether the address is an 802.3
1227 * (link) address, or an appletalk address. If it's an
1228 * 802.3 address, the packet can just go out to the network
1229 * through PAT, if it's an appletalk address, AT->802.3 address
1230 * resolution needs to be done.
1231 * If 802.3 address is known, strip off the flag and 802.3
1232 * address, and prepend 802.2 and 802.3 headers.
1236 addr_flag
= *(u_char
*)gbuf_rptr(m
);
1240 switch (addr_flag
) {
1241 case AT_ADDR_NO_LOOP
:
1246 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
1247 gbuf_rinc(m
,sizeof(struct atalk_addr
));
1249 dest_at_addr
= *(struct atalk_addr
*)addr
;
1253 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
1254 gbuf_rinc(m
,sizeof(struct etalk_addr
));
1256 dest_addr
= *(struct etalk_addr
*)addr
;
1259 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
1260 ("ddp_router_output: Unknown addr_flag = 0x%x\n", addr_flag
));
1262 gbuf_freel(m
); /* unknown address type, chuck it */
1268 /* At this point, rptr points to ddp header for sure */
1269 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
1270 /* see if this is a ZIP packet that we need
1271 * to let through even though network is
1274 if (zip_type_packet(m
) == 0) {
1280 ifID
->stats
.xmit_packets
++;
1281 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
1282 snmpStats
.dd_outLong
++;
1284 switch (addr_flag
) {
1285 case AT_ADDR_NO_LOOP
:
1288 * we don't want elap to be looking into ddp header, so
1289 * it doesn't know net#, consequently can't do
1290 * AMT_LOOKUP. That task left to aarp now.
1292 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
1295 pat_output(ifID
, m
, &dest_addr
, 0);
1301 } /* ddp_router_output */
1303 /*****************************************/
1305 void rt_delete(NetStop
, NetStart
)
1306 unsigned short NetStop
;
1307 unsigned short NetStart
;
1312 if ((found
= rt_bdelete(NetStop
, NetStart
)) != 0) {
1313 bzero(found
, sizeof(RT_entry
));
1314 found
->right
= RT_table_freelist
;
1315 RT_table_freelist
= found
;
1320 int ddp_AURPfuncx(code
, param
, node
)
1325 extern void rtmp_timeout();
1326 extern void rtmp_send_port();
1331 case AURPCODE_DATAPKT
: /* data packet */
1333 dPrintf(D_M_DDP
, D_L_TRACE
, ("ddp_AURPfuncx: data, 0x%x, %d\n",
1334 (u_int
) aurp_ifID
, node
));
1336 ddp_input((gbuf_t
*)param
, aurp_ifID
);
1338 gbuf_freem((gbuf_t
*)param
);
1341 case AURPCODE_REG
: /* register/deregister */
1344 ddp_AURPsendx
= (void(*)())param
;
1347 /* register AURP callback function */
1350 for (k
=(IFID_HOME
+1); k
< IF_TOTAL_MAX
; k
++) {
1351 if (ifID_table
[k
] == 0) {
1352 aurp_ifID
= &at_interfaces
[k
];
1353 aurp_ifID
->ifFlags
= RTR_XNET_PORT
;
1354 ddp_add_if(aurp_ifID
);
1355 aurp_ifID
->ifState
= LAP_ONLINE
;
1356 aurp_ifID
->ifRoutingState
= PORT_ONLINE
;
1357 dPrintf(D_M_DDP
, D_L_TRACE
,
1358 ("ddp_AURPfuncx: on, 0x%x\n",
1359 (u_int
) aurp_ifID
));
1361 ddp_AURPsendx(AURPCODE_DEBUGINFO
,
1362 &dbgBits
, aurp_ifID
->ifPort
);
1369 /* deregister AURP callback function */
1371 rtmp_purge(aurp_ifID
);
1372 ddp_rem_if(aurp_ifID
);
1373 aurp_ifID
->ifState
= LAP_OFFLINE
;
1374 aurp_ifID
->ifRoutingState
= PORT_OFFLINE
;
1375 dPrintf(D_M_DDP
, D_L_TRACE
,
1376 ("ddp_AURPfuncx: off, 0x%x\n", (u_int
) aurp_ifID
));
1382 case AURPCODE_AURPPROTO
: /* proto type - AURP */
1384 aurp_ifID
->ifFlags
|= AT_IFF_AURP
;
1393 /* checks to see if address of packet is for one of our interfaces
1394 returns *ifID if it's for us, NULL if not
1396 at_ifaddr_t
*forUs(ddp
)
1397 register at_ddp_t
*ddp
;
1401 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
1402 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1403 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)
1405 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1406 ("pkt was for port %d\n", ifID
->ifPort
));
1412 return((at_ifaddr_t
*)NULL
);