1 /*
2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
3 * All Rights Reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
14 *
15 * Research Systems Unix Group
16 * The University of Michigan
17 * c/o Mike Clark
18 * 535 W. William Street
19 * Ann Arbor, Michigan
20 * +1-313-763-0525
21 * netatalk@itd.umich.edu
22 */
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/mbuf.h>
27 #include <sys/socket.h>
28 #include <sys/socketvar.h>
29
30 #include <net/if.h>
31 #include <net/route.h>
32
33 #undef s_net
34
35 #include <netatalk/at.h>
36 #include <netatalk/at_var.h>
37 #include <netatalk/ddp.h>
38 #include <netatalk/ddp_var.h>
39 #include <netatalk/at_extern.h>
40
41 int ddp_cksum = 1;
42
43 int
44 ddp_output( struct mbuf *m, struct socket *so)
45 {
46 struct ddpehdr *deh;
47 struct ddpcb *ddp = sotoddpcb( so );
48
49 M_PREPEND( m, sizeof( struct ddpehdr ), M_WAIT );
50
51 deh = mtod( m, struct ddpehdr *);
52 deh->deh_pad = 0;
53 deh->deh_hops = 0;
54
55 deh->deh_len = m->m_pkthdr.len;
56
57 deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
58 deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
59 deh->deh_dport = ddp->ddp_fsat.sat_port;
60 deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
61 deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
62 deh->deh_sport = ddp->ddp_lsat.sat_port;
63
64 /*
65 * The checksum calculation is done after all of the other bytes have
66 * been filled in.
67 */
68 if ( ddp_cksum ) {
69 deh->deh_sum = at_cksum( m, sizeof( int ));
70 } else {
71 deh->deh_sum = 0;
72 }
73 deh->deh_bytes = htonl( deh->deh_bytes );
74
75 #ifdef NETATALK_DEBUG
76 printf ("ddp_output: from %d.%d:%d to %d.%d:%d\n",
77 ntohs(deh->deh_snet), deh->deh_snode, deh->deh_sport,
78 ntohs(deh->deh_dnet), deh->deh_dnode, deh->deh_dport);
79 #endif
80 return( ddp_route( m, &ddp->ddp_route ));
81 }
82
83 u_short
84 at_cksum( struct mbuf *m, int skip)
85 {
86 u_char *data, *end;
87 u_long cksum = 0;
88
89 for (; m; m = m->m_next ) {
90 for ( data = mtod( m, u_char * ), end = data + m->m_len; data < end;
91 data++ ) {
92 if ( skip ) {
93 skip--;
94 continue;
95 }
96 cksum = ( cksum + *data ) << 1;
97 if ( cksum & 0x00010000 ) {
98 cksum++;
99 }
100 cksum &= 0x0000ffff;
101 }
102 }
103
104 if ( cksum == 0 ) {
105 cksum = 0x0000ffff;
106 }
107 return( (u_short)cksum );
108 }
109
110 int
111 ddp_route( struct mbuf *m, struct route *ro)
112 {
113 struct sockaddr_at gate;
114 struct elaphdr *elh;
115 struct mbuf *m0;
116 struct at_ifaddr *aa = NULL;
117 struct ifnet *ifp = NULL;
118 u_short net;
119
120 #if 0
121 /* Check for net zero, node zero ("myself") */
122 if (satosat(&ro->ro_dst)->sat_addr.s_net == ATADDR_ANYNET
123 && satosat(&ro->ro_dst)->sat_addr.s_node == ATADDR_ANYNODE) {
124 /* Find the loopback interface */
125 }
126 #endif
127
128 /*
129 * if we have a route, find the ifa that refers to this route.
130 * I.e The ifa used to get to the gateway.
131 */
132 if ( (ro->ro_rt == NULL)
133 || ( ro->ro_rt->rt_ifa == NULL )
134 || ( (ifp = ro->ro_rt->rt_ifa->ifa_ifp) == NULL )) {
135 rtalloc(ro);
136 }
137 if ( (ro->ro_rt != NULL)
138 && ( ro->ro_rt->rt_ifa )
139 && ( ifp = ro->ro_rt->rt_ifa->ifa_ifp )) {
140 net = ntohs(satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net);
141 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
142 if (((net == 0) || (aa->aa_ifp == ifp)) &&
143 net >= ntohs( aa->aa_firstnet ) &&
144 net <= ntohs( aa->aa_lastnet )) {
145 break;
146 }
147 }
148 } else {
149 m_freem( m );
150 #ifdef NETATALK_DEBUG
151 if (ro->ro_rt == NULL)
152 printf ("ddp_route: no ro_rt.\n");
153 else if (ro->ro_rt->rt_ifa == NULL)
154 printf ("ddp_route: no ro_rt->rt_ifa\n");
155 else
156 printf ("ddp_route: no ro_rt->rt_ifa->ifa_ifp\n");
157 #endif
158 return( ENETUNREACH );
159 }
160
161 if ( aa == NULL ) {
162 #ifdef NETATALK_DEBUG
163 printf( "ddp_route: no atalk address found for %s%d\n",
164 ifp->if_name, ifp->if_unit);
165 #endif
166 m_freem( m );
167 return( ENETUNREACH );
168 }
169
170 /*
171 * if the destination address is on a directly attached node use that,
172 * else use the official gateway.
173 */
174 if ( ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) >=
175 ntohs( aa->aa_firstnet ) &&
176 ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) <=
177 ntohs( aa->aa_lastnet )) {
178 gate = *satosat( &ro->ro_dst );
179 } else {
180 gate = *satosat( ro->ro_rt->rt_gateway );
181 }
182
183 /*
184 * There are several places in the kernel where data is added to
185 * an mbuf without ensuring that the mbuf pointer is aligned.
186 * This is bad for transition routing, since phase 1 and phase 2
187 * packets end up poorly aligned due to the three byte elap header.
188 */
189 if ( !(aa->aa_flags & AFA_PHASE2) ) {
190 MGET( m0, M_WAIT, MT_HEADER );
191 if ( m0 == 0 ) {
192 m_freem( m );
193 printf("ddp_route: no buffers\n");
194 return( ENOBUFS );
195 }
196 m0->m_next = m;
197 /* XXX perhaps we ought to align the header? */
198 m0->m_len = SZ_ELAPHDR;
199 m = m0;
200
201 elh = mtod( m, struct elaphdr *);
202 elh->el_snode = satosat( &aa->aa_addr )->sat_addr.s_node;
203 elh->el_type = ELAP_DDPEXTEND;
204 elh->el_dnode = gate.sat_addr.s_node;
205 }
206 ro->ro_rt->rt_use++;
207
208 #ifdef NETATALK_DEBUG
209 printf ("ddp_route: from %d.%d to %d.%d, via %d.%d (%s%d)\n",
210 ntohs(satosat(&aa->aa_addr)->sat_addr.s_net),
211 satosat(&aa->aa_addr)->sat_addr.s_node,
212 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net),
213 satosat(&ro->ro_dst)->sat_addr.s_node,
214 ntohs(gate.sat_addr.s_net),
215 gate.sat_addr.s_node,
216 ifp->if_name, ifp->if_unit);
217 #endif
218
219 /* short-circuit the output if we're sending this to ourself */
220 if ((satosat(&aa->aa_addr)->sat_addr.s_net == satosat(&ro->ro_dst)->sat_addr.s_net) &&
221 (satosat(&aa->aa_addr)->sat_addr.s_node == satosat(&ro->ro_dst)->sat_addr.s_node))
222 {
223 return (if_simloop(ifp, m, (struct sockaddr *)&gate, 0));
224 }
225
226 return((*ifp->if_output)( ifp,
227 m, (struct sockaddr *)&gate, NULL)); /* XXX */
228 }
Cache object: 0e11788cd4c967605bc849a0053af91e
|