3 Ultrix PacketFilter interface code. */
6 * Copyright (c) 1996-2000 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38 * To learn more about the Internet Software Consortium, see
39 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
40 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
41 * ``http://www.nominum.com''.
45 static char copyright
[] =
46 "$Id: upf.c,v 1.17 2000/03/17 03:59:02 mellon Exp $ Copyright (c) 1996-2000 The Internet Software Consortium. All rights reserved.\n";
50 #if defined (USE_UPF_SEND) || defined (USE_UPF_RECEIVE)
51 #include <sys/ioctl.h>
54 #include <net/pfilt.h>
55 #include <netinet/in_systm.h>
56 #include "includes/netinet/ip.h"
57 #include "includes/netinet/udp.h"
58 #include "includes/netinet/if_ether.h"
60 /* Reinitializes the specified interface after an address change. This
61 is not required for packet-filter APIs. */
64 void if_reinitialize_send (info
)
65 struct interface_info
*info
;
70 #ifdef USE_UPF_RECEIVE
71 void if_reinitialize_receive (info
)
72 struct interface_info
*info
;
77 /* Called by get_interface_list for each interface that's discovered.
78 Opens a packet filter for each interface and adds it to the select
81 int if_register_upf (info
)
82 struct interface_info
*info
;
89 /* Open a UPF device */
92 snprintf(filename
, sizeof(filename
), "/dev/pf/pfilt%d", b
);
94 sprintf(filename
, "/dev/pf/pfilt%d", b
);
96 sock
= open (filename
, O_RDWR
, 0);
101 log_fatal ("Can't find free upf: %m");
108 /* Set the UPF device to point at this interface. */
109 if (ioctl (sock
, EIOCSETIF
, info
-> ifp
) < 0)
110 log_fatal ("Can't attach interface %s to upf device %s: %m",
111 info
-> name
, filename
);
113 /* Get the hardware address. */
114 if (ioctl (sock
, EIOCDEVP
, ¶m
) < 0)
115 log_fatal ("Can't get interface %s hardware address: %m",
118 /* We only know how to do ethernet. */
119 if (param
.end_dev_type
!= ENDT_10MB
)
120 log_fatal ("Invalid device type on network interface %s: %d",
121 info
-> name
, param
.end_dev_type
);
123 if (param
.end_addr_len
!= 6)
124 log_fatal ("Invalid hardware address length on %s: %d",
125 info
-> name
, param
.end_addr_len
);
127 info
-> hw_address
.hlen
= 7;
128 info
-> hw_address
.hbuf
[0] = ARPHRD_ETHER
;
129 memcpy (&info
-> hw_address
.hbuf
[1], param
.end_addr
, 6);
133 #endif /* USE_UPF_SEND || USE_UPF_RECEIVE */
136 void if_register_send (info
)
137 struct interface_info
*info
;
139 /* If we're using the upf API for sending and receiving,
140 we don't need to register this interface twice. */
141 #ifndef USE_UPF_RECEIVE
142 info
-> wfdesc
= if_register_upf (info
, interface
);
144 info
-> wfdesc
= info
-> rfdesc
;
146 if (!quiet_interface_discovery
)
147 log_info ("Sending on UPF/%s/%s%s%s",
149 print_hw_addr (info
-> hw_address
.hbuf
[0],
150 info
-> hw_address
.hlen
- 1,
151 &info
-> hw_address
.hbuf
[1]),
152 (info
-> shared_network
? "/" : ""),
153 (info
-> shared_network
?
154 info
-> shared_network
-> name
: ""));
157 void if_deregister_send (info
)
158 struct interface_info
*info
;
160 #ifndef USE_UPF_RECEIVE
161 close (info
-> wfdesc
);
164 if (!quiet_interface_discovery
)
165 log_info ("Disabling output on UPF/%s/%s%s%s",
167 print_hw_addr (info
-> hw_address
.hbuf
[0],
168 info
-> hw_address
.hlen
- 1,
169 &info
-> hw_address
.hbuf
[1]),
170 (info
-> shared_network
? "/" : ""),
171 (info
-> shared_network
?
172 info
-> shared_network
-> name
: ""));
174 #endif /* USE_UPF_SEND */
176 #ifdef USE_UPF_RECEIVE
177 /* Packet filter program...
178 XXX Changes to the filter program may require changes to the constant
179 offsets used in if_register_send to patch the UPF program! XXX */
182 void if_register_receive (info
)
183 struct interface_info
*info
;
190 /* Open a UPF device and hang it on this interface... */
191 info
-> rfdesc
= if_register_upf (info
);
193 /* Allow the copyall flag to be set... */
194 if (ioctl(info
-> rfdesc
, EIOCALLOWCOPYALL
, &flag
) < 0)
195 log_fatal ("Can't set ALLOWCOPYALL: %m");
197 /* Clear all the packet filter mode bits first... */
198 flag
= (ENHOLDSIG
| ENBATCH
| ENTSTAMP
| ENPROMISC
|
199 ENNONEXCL
| ENCOPYALL
);
200 if (ioctl (info
-> rfdesc
, EIOCMBIC
, &flag
) < 0)
201 log_fatal ("Can't clear pfilt bits: %m");
203 /* Set the ENBATCH and ENCOPYALL bits... */
204 bits
= ENBATCH
| ENCOPYALL
;
205 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
206 log_fatal ("Can't set ENBATCH|ENCOPYALL: %m");
208 /* Set up the UPF filter program. */
209 /* XXX Unlike the BPF filter program, this one won't work if the
210 XXX IP packet is fragmented or if there are options on the IP
213 pf
.enf_FilterLen
= 0;
215 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHWORD
+ 6;
216 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
217 pf
.enf_Filter
[pf
.enf_FilterLen
++] = htons (ETHERTYPE_IP
);
218 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHLIT
;
219 pf
.enf_Filter
[pf
.enf_FilterLen
++] = htons (IPPROTO_UDP
);
220 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHWORD
+ 11;
221 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHLIT
+ ENF_AND
;
222 pf
.enf_Filter
[pf
.enf_FilterLen
++] = htons (0xFF);
223 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_CAND
;
224 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHWORD
+ 18;
225 pf
.enf_Filter
[pf
.enf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
226 pf
.enf_Filter
[pf
.enf_FilterLen
++] = local_port
;
228 if (ioctl (info
-> rfdesc
, EIOCSETF
, &pf
) < 0)
229 log_fatal ("Can't install packet filter program: %m");
230 if (!quiet_interface_discovery
)
231 log_info ("Listening on UPF/%s/%s%s%s",
233 print_hw_addr (info
-> hw_address
.hbuf
[0],
234 info
-> hw_address
.hlen
- 1,
235 &info
-> hw_address
.hbuf
[1]),
236 (info
-> shared_network
? "/" : ""),
237 (info
-> shared_network
?
238 info
-> shared_network
-> name
: ""));
241 void if_deregister_receive (info
)
242 struct interface_info
*info
;
244 close (info
-> rfdesc
);
246 if (!quiet_interface_discovery
)
247 log_info ("Disabling input on UPF/%s/%s%s%s",
249 print_hw_addr (info
-> hw_address
.hbuf
[0],
250 info
-> hw_address
.hlen
- 1,
251 &info
-> hw_address
.hbuf
[1]),
252 (info
-> shared_network
? "/" : ""),
253 (info
-> shared_network
?
254 info
-> shared_network
-> name
: ""));
256 #endif /* USE_UPF_RECEIVE */
259 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
260 struct interface_info
*interface
;
261 struct packet
*packet
;
262 struct dhcp_packet
*raw
;
265 struct sockaddr_in
*to
;
266 struct hardware
*hto
;
269 unsigned char buf
[256];
270 struct iovec iov
[2];
273 if (!strcmp (interface
-> name
, "fallback"))
274 return send_fallback (interface
, packet
, raw
,
277 /* Assemble the headers... */
278 assemble_hw_header (interface
, buf
, &bufp
, hto
);
279 assemble_udp_ip_header (interface
, buf
, &bufp
, from
.s_addr
,
280 to
-> sin_addr
.s_addr
, to
-> sin_port
,
281 (unsigned char *)raw
, len
);
284 iov
[0].iov_base
= (char *)buf
;
285 iov
[0].iov_len
= bufp
;
286 iov
[1].iov_base
= (char *)raw
;
287 iov
[1].iov_len
= len
;
289 result
= writev(interface
-> wfdesc
, iov
, 2);
291 log_error ("send_packet: %m");
294 #endif /* USE_UPF_SEND */
296 #ifdef USE_UPF_RECEIVE
297 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
298 struct interface_info
*interface
;
301 struct sockaddr_in
*from
;
302 struct hardware
*hfrom
;
307 unsigned char ibuf
[1500 + sizeof (struct enstamp
)];
310 length
= read (interface
-> rfdesc
, ibuf
, sizeof ibuf
);
314 bufix
= sizeof (struct enstamp
);
315 /* Decode the physical header... */
316 offset
= decode_hw_header (interface
, ibuf
, bufix
, hfrom
);
318 /* If a physical layer checksum failed (dunno of any
319 physical layer that supports this, but WTH), skip this
328 /* Decode the IP and UDP headers... */
329 offset
= decode_udp_ip_header (interface
, ibuf
, bufix
,
330 from
, (unsigned char *)0, length
);
332 /* If the IP or UDP checksum was bad, skip the packet... */
339 /* Copy out the data in the packet... */
340 memcpy (buf
, &ibuf
[bufix
], length
);
344 int can_unicast_without_arp (ip
)
345 struct interface_info
*ip
;
350 int can_receive_unicast_unconfigured (ip
)
351 struct interface_info
*ip
;
356 void maybe_setup_fallback ()
359 struct interface_info
*fbi
;
360 fbi
= setup_fallback ();
362 if_register_fallback (fbi
);
364 fbi
-> type
= dhcp_type_interface
;
365 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
367 fallback_discard
, 0, 0);
368 if (status
!= ISC_R_SUCCESS
)
369 log_fatal ("Can't register I/O handle for %s: %s",
370 fbi
-> name
, isc_result_totext (status
));