1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/kernel.h>
61#include <sys/malloc.h>
62#include <sys/mbuf.h>
63#include <sys/socket.h>
64#include <sys/sockio.h>
65#include <sys/sysctl.h>
66
67#include <net/if.h>
68#include <net/route.h>
69#include <net/if_llc.h>
70#include <net/if_dl.h>
71#include <net/if_types.h>
72#include <netinet/if_ether.h>
73
74#include <sys/socketvar.h>
75
76#include <net/dlil.h>
77#include <netat/at_pat.h>
78#if NETAT
79extern struct ifqueue atalkintrq;
80#endif
81
82
83#if BRIDGE
84#include <net/bridge.h>
85#endif
86
87
88#if NVLAN > 0
89#include <net/if_vlan_var.h>
90#endif
91
92struct dl_es_at_entry
93{
94 struct ifnet *ifp;
95 int ref_count;
96};
97
98
99int at_ether_input(struct mbuf *m, char *frame_header, struct ifnet *ifp,
100 u_long protocol_family, int sync_ok);
101int ether_pre_output(struct ifnet *ifp, u_long protocol_family, struct mbuf **m0,
102 const struct sockaddr *dst_netaddr, caddr_t route, char *type, char *edst);
103int ether_prmod_ioctl(u_long protocol_family, struct ifnet *ifp, u_long command,
104 caddr_t data);
105int ether_attach_at(struct ifnet *ifp);
106void ether_detach_at(struct ifnet *ifp);
107
108
109
110
111
112
113#define MAX_EN_COUNT 30
114
115static struct dl_es_at_entry en_at_array[MAX_EN_COUNT];
116
117
118
119
120
121
122int
123at_ether_input(
124 struct mbuf *m,
125 __unused char *frame_header,
126 __unused struct ifnet *ifp,
127 __unused u_long protocol_family,
128 __unused int sync_ok)
129
130{
131
132
133
134
135
136
137
138 m->m_data -= sizeof(struct ether_header);
139 m->m_len += sizeof(struct ether_header);
140 m->m_pkthdr.len += sizeof(struct ether_header);
141 proto_input(PF_APPLETALK, m);
142
143 return 0;
144}
145
146
147
148int
149ether_pre_output(
150 struct ifnet *ifp,
151 __unused u_long protocol_family,
152 struct mbuf **m0,
153 const struct sockaddr *dst_netaddr,
154 __unused caddr_t route,
155 char *type,
156 char *edst)
157{
158 register struct mbuf *m = *m0;
159 register struct ether_header *eh;
160 int hlen;
161
162
163
164 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
165 return ENETDOWN;
166
167 hlen = ETHER_HDR_LEN;
168
169
170
171
172 m->m_flags |= M_LOOP;
173
174 switch (dst_netaddr->sa_family) {
175 case AF_UNSPEC:
176 m->m_flags &= ~M_LOOP;
177 eh = (struct ether_header *)dst_netaddr->sa_data;
178 (void)memcpy(edst, eh->ether_dhost, 6);
179 *(u_short *)type = eh->ether_type;
180 break;
181
182
183 case AF_APPLETALK:
184 {
185 eh = (struct ether_header *)dst_netaddr->sa_data;
186 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, 6);
187
188 *(u_short *)type = m->m_pkthdr.len;
189 }
190 break;
191
192
193 default:
194 kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
195 dst_netaddr->sa_family);
196
197 return EAFNOSUPPORT;
198 }
199
200 return (0);
201}
202
203
204
205
206
207int
208ether_prmod_ioctl(
209 __unused u_long protocol_family,
210 struct ifnet *ifp,
211 u_long command,
212 caddr_t data)
213{
214 struct ifreq *ifr = (struct ifreq *) data;
215 int error = 0;
216
217 switch (command) {
218
219 case SIOCSIFADDR:
220 if ((ifp->if_flags & IFF_RUNNING) == 0) {
221 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
222 dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
223 }
224
225 break;
226
227 case SIOCGIFADDR:
228 ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
229 break;
230
231 case SIOCSIFMTU:
232
233
234
235 if (ifr->ifr_mtu > ETHERMTU) {
236 error = EINVAL;
237 } else {
238 ifp->if_mtu = ifr->ifr_mtu;
239 }
240 break;
241
242 default:
243 return EOPNOTSUPP;
244 }
245
246
247 return (error);
248}
249
250
251
252int
253ether_attach_at(
254 struct ifnet *ifp)
255{
256 struct dlil_proto_reg_str reg;
257 struct dlil_demux_desc desc;
258 struct dlil_demux_desc desc2;
259 int stat;
260 int first_empty;
261 int i;
262 u_int8_t atalk_snap[5] = {0x08, 0x00, 0x07, 0x80, 0x9b};
263 u_int8_t aarp_snap[5] = {0x00, 0x00, 0x00, 0x80, 0xf3};
264
265 first_empty = MAX_EN_COUNT;
266 for (i=0; i < MAX_EN_COUNT; i++) {
267 if (en_at_array[i].ifp == 0)
268 first_empty = i;
269
270 if (en_at_array[i].ifp == ifp) {
271 en_at_array[i].ref_count++;
272 return 0;
273 }
274 }
275
276 if (first_empty == MAX_EN_COUNT)
277 return ENOMEM;
278
279 bzero(®, sizeof(reg));
280 bzero(&desc, sizeof(desc));
281 bzero(&desc2, sizeof(desc2));
282
283 TAILQ_INIT(®.demux_desc_head);
284 reg.interface_family = ifp->if_family;
285 reg.unit_number = ifp->if_unit;
286 reg.input = at_ether_input;
287 reg.pre_output = ether_pre_output;
288 reg.ioctl = ether_prmod_ioctl;
289 reg.protocol_family = PF_APPLETALK;
290
291 desc.type = DLIL_DESC_SNAP;
292 desc.native_type = atalk_snap;
293 desc.variants.native_type_length = sizeof(atalk_snap);
294 TAILQ_INSERT_TAIL(®.demux_desc_head, &desc, next);
295
296 desc2.type = DLIL_DESC_SNAP;
297 desc2.native_type = aarp_snap;
298 desc2.variants.native_type_length = sizeof(aarp_snap);
299 TAILQ_INSERT_TAIL(®.demux_desc_head, &desc2, next);
300
301 stat = dlil_attach_protocol(®);
302 if (stat) {
303 printf("WARNING: ether_attach_at can't attach at to interface\n");
304 return stat;
305 }
306
307 en_at_array[first_empty].ifp = ifp;
308 en_at_array[first_empty].ref_count = 1;
309
310 return 0;
311}
312
313
314void
315ether_detach_at(struct ifnet *ifp)
316{
317 int i;
318
319 for (i=0; i < MAX_EN_COUNT; i++) {
320 if (en_at_array[i].ifp == ifp)
321 break;
322 }
323
324 if (i < MAX_EN_COUNT) {
325 if (en_at_array[i].ref_count > 1)
326 en_at_array[i].ref_count--;
327 else {
328 if (en_at_array[i].ref_count == 1) {
329 dlil_detach_protocol(ifp, PF_APPLETALK);
330 en_at_array[i].ifp = 0;
331 }
332 }
333 }
334}
335