3 Ultrix PacketFilter interface code. */
6 * Copyright (c) 1996-2002 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.21.2.1 2002/11/17 02:27:00 dhankins Exp $ Copyright (c) 1996-2002 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;
268 unsigned hbufp = 0, ibufp = 0;
271 struct iovec iov [3];
275 if (!strcmp (interface -> name, "fallback"))
276 return send_fallback (interface, packet, raw,
279 /* Assemble the headers... */
280 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
281 assemble_udp_ip_header (interface,
282 (unsigned char *)ip, &ibufp, from.s_addr,
283 to -> sin_addr.s_addr, to -> sin_port,
284 (unsigned char *)raw, len);
287 iov [0].iov_base = ((char *)hw);
288 iov [0].iov_len = hbufp;
289 iov [1].iov_base = ((char *)ip);
290 iov [1].iov_len = ibufp;
291 iov [2].iov_base = (char *)raw;
292 iov [2].iov_len = len;
294 result = writev(interface -> wfdesc, iov, 3);
296 log_error ("send_packet: %m");
299 #endif /* USE_UPF_SEND */
301 #ifdef USE_UPF_RECEIVE
302 ssize_t receive_packet (interface, buf, len, from, hfrom)
303 struct interface_info *interface;
306 struct sockaddr_in *from;
307 struct hardware *hfrom;
312 unsigned char ibuf [1500 + sizeof (struct enstamp)];
315 length = read (interface -> rfdesc, ibuf, sizeof ibuf);
319 bufix = sizeof (struct enstamp);
320 /* Decode the physical header... */
321 offset = decode_hw_header (interface, ibuf, bufix, hfrom);
323 /* If a physical layer checksum failed (dunno of any
324 physical layer that supports this, but WTH), skip this
333 /* Decode the IP and UDP headers... */
334 offset = decode_udp_ip_header (interface, ibuf, bufix,
335 from, (unsigned char *)0, length);
337 /* If the IP or UDP checksum was bad, skip the packet... */
344 /* Copy out the data in the packet... */
345 memcpy (buf, &ibuf [bufix], length);
349 int can_unicast_without_arp (ip)
350 struct interface_info *ip;
355 int can_receive_unicast_unconfigured (ip)
356 struct interface_info *ip;
361 int supports_multiple_interfaces (ip)
362 struct interface_info *ip;
367 void maybe_setup_fallback ()
370 struct interface_info *fbi = (struct interface_info *)0;
371 if (setup_fallback (&fbi, MDL)) {
372 if_register_fallback (fbi);
373 status = omapi_register_io_object ((omapi_object_t *)fbi,
375 fallback_discard, 0, 0);
376 if (status != ISC_R_SUCCESS)
377 log_fatal ("Can't register I/O handle for %s: %s",
378 fbi -> name, isc_result_totext (status));
379 interface_dereference (&fbi, MDL);