Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / common / nit.c
1 /* nit.c
2
3    Network Interface Tap (NIT) network interface code, by Ted Lemon
4    with one crucial tidbit of help from Stu Grossmen. */
5
6 /*
7  * Copyright (c) 1996-2002 Internet Software Consortium.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of The Internet Software Consortium nor the names
20  *    of its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * This software has been written for the Internet Software Consortium
38  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
39  * To learn more about the Internet Software Consortium, see
40  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
41  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
42  * ``http://www.nominum.com''.
43  */
44
45 #ifndef lint
46 static char copyright[] =
47 "$Id: nit.c,v 1.34.2.1 2002/11/17 02:26:58 dhankins Exp $ Copyright (c) 1996-2002 The Internet Software Consortium.  All rights reserved.\n";
48 #endif /* not lint */
49
50 #include "dhcpd.h"
51 #if defined (USE_NIT_SEND) || defined (USE_NIT_RECEIVE)
52 #include <sys/ioctl.h>
53 #include <sys/uio.h>
54
55 #include <sys/time.h>
56 #include <net/nit.h>
57 #include <net/nit_if.h>
58 #include <net/nit_pf.h>
59 #include <net/nit_buf.h>
60 #include <sys/stropts.h>
61 #include <net/packetfilt.h>
62
63 #include <netinet/in_systm.h>
64 #include "includes/netinet/ip.h"
65 #include "includes/netinet/udp.h"
66 #include "includes/netinet/if_ether.h"
67
68 /* Reinitializes the specified interface after an address change.   This
69    is not required for packet-filter APIs. */
70
71 #ifdef USE_NIT_SEND
72 void if_reinitialize_send (info)
73         struct interface_info *info;
74 {
75 }
76 #endif
77
78 #ifdef USE_NIT_RECEIVE
79 void if_reinitialize_receive (info)
80         struct interface_info *info;
81 {
82 }
83 #endif
84
85 /* Called by get_interface_list for each interface that's discovered.
86    Opens a packet filter for each interface and adds it to the select
87    mask. */
88
89 int if_register_nit (info)
90         struct interface_info *info;
91 {
92         int sock;
93         char filename[50];
94         struct ifreq ifr;
95         struct strioctl sio;
96
97         /* Open a NIT device */
98         sock = open ("/dev/nit", O_RDWR);
99         if (sock < 0)
100                 log_fatal ("Can't open NIT device for %s: %m", info -> name);
101
102         /* Set the NIT device to point at this interface. */
103         sio.ic_cmd = NIOCBIND;
104         sio.ic_len = sizeof *(info -> ifp);
105         sio.ic_dp = (char *)(info -> ifp);
106         sio.ic_timout = INFTIM;
107         if (ioctl (sock, I_STR, &sio) < 0)
108                 log_fatal ("Can't attach interface %s to nit device: %m",
109                        info -> name);
110
111         /* Get the low-level address... */
112         sio.ic_cmd = SIOCGIFADDR;
113         sio.ic_len = sizeof ifr;
114         sio.ic_dp = (char *)&ifr;
115         sio.ic_timout = INFTIM;
116         if (ioctl (sock, I_STR, &sio) < 0)
117                 log_fatal ("Can't get physical layer address for %s: %m",
118                        info -> name);
119
120         /* XXX code below assumes ethernet interface! */
121         info -> hw_address.hlen = 7;
122         info -> hw_address.hbuf [0] = ARPHRD_ETHER;
123         memcpy (&info -> hw_address.hbuf [1],
124                 ifr.ifr_ifru.ifru_addr.sa_data, 6);
125
126         if (ioctl (sock, I_PUSH, "pf") < 0)
127                 log_fatal ("Can't push packet filter onto NIT for %s: %m",
128                        info -> name);
129
130         return sock;
131 }
132 #endif /* USE_NIT_SEND || USE_NIT_RECEIVE */
133
134 #ifdef USE_NIT_SEND
135 void if_register_send (info)
136         struct interface_info *info;
137 {
138         /* If we're using the nit API for sending and receiving,
139            we don't need to register this interface twice. */
140 #ifndef USE_NIT_RECEIVE
141         struct packetfilt pf;
142         struct strioctl sio;
143
144         info -> wfdesc = if_register_nit (info);
145
146         pf.Pf_Priority = 0;
147         pf.Pf_FilterLen = 1;
148         pf.Pf_Filter [0] = ENF_PUSHZERO;
149
150         /* Set up an NIT filter that rejects everything... */
151         sio.ic_cmd = NIOCSETF;
152         sio.ic_len = sizeof pf;
153         sio.ic_dp = (char *)&pf;
154         sio.ic_timout = INFTIM;
155         if (ioctl (info -> wfdesc, I_STR, &sio) < 0)
156                 log_fatal ("Can't set NIT filter: %m");
157 #else
158         info -> wfdesc = info -> rfdesc;
159 #endif
160         if (!quiet_interface_discovery)
161                 log_info ("Sending on   NIT/%s%s%s",
162                       print_hw_addr (info -> hw_address.hbuf [0],
163                                      info -> hw_address.hlen - 1,
164                                      &info -> hw_address.hbuf [1]),
165                       (info -> shared_network ? "/" : ""),
166                       (info -> shared_network ?
167                        info -> shared_network -> name : ""));
168 }
169
170 void if_deregister_send (info)
171         struct interface_info *info;
172 {
173         /* If we're using the nit API for sending and receiving,
174            we don't need to register this interface twice. */
175 #ifndef USE_NIT_RECEIVE
176         close (info -> wfdesc);
177 #endif
178         info -> wfdesc = -1;
179         if (!quiet_interface_discovery)
180                 log_info ("Disabling output on NIT/%s%s%s",
181                       print_hw_addr (info -> hw_address.hbuf [0],
182                                      info -> hw_address.hlen - 1,
183                                      &info -> hw_address.hbuf [1]),
184                       (info -> shared_network ? "/" : ""),
185                       (info -> shared_network ?
186                        info -> shared_network -> name : ""));
187 }
188 #endif /* USE_NIT_SEND */
189
190 #ifdef USE_NIT_RECEIVE
191 /* Packet filter program...
192    XXX Changes to the filter program may require changes to the constant
193    offsets used in if_register_send to patch the NIT program! XXX */
194
195 void if_register_receive (info)
196         struct interface_info *info;
197 {
198         int flag = 1;
199         u_int32_t x;
200         struct packetfilt pf;
201         struct strioctl sio;
202         u_int16_t addr [2];
203         struct timeval t;
204
205         /* Open a NIT device and hang it on this interface... */
206         info -> rfdesc = if_register_nit (info);
207
208         /* Set the snap length to 0, which means always take the whole
209            packet. */
210         x = 0;
211         if (ioctl (info -> rfdesc, NIOCSSNAP, &x) < 0)
212                 log_fatal ("Can't set NIT snap length on %s: %m", info -> name);
213
214         /* Set the stream to byte stream mode */
215         if (ioctl (info -> rfdesc, I_SRDOPT, RMSGN) != 0)
216                 log_info ("I_SRDOPT failed on %s: %m", info -> name);
217
218 #if 0
219         /* Push on the chunker... */
220         if (ioctl (info -> rfdesc, I_PUSH, "nbuf") < 0)
221                 log_fatal ("Can't push chunker onto NIT STREAM: %m");
222
223         /* Set the timeout to zero. */
224         t.tv_sec = 0;
225         t.tv_usec = 0;
226         if (ioctl (info -> rfdesc, NIOCSTIME, &t) < 0)
227                 log_fatal ("Can't set chunk timeout: %m");
228 #endif
229
230         /* Ask for no header... */
231         x = 0;
232         if (ioctl (info -> rfdesc, NIOCSFLAGS, &x) < 0)
233                 log_fatal ("Can't set NIT flags on %s: %m", info -> name);
234
235         /* Set up the NIT filter program. */
236         /* XXX Unlike the BPF filter program, this one won't work if the
237            XXX IP packet is fragmented or if there are options on the IP
238            XXX header. */
239         pf.Pf_Priority = 0;
240         pf.Pf_FilterLen = 0;
241
242         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 6;
243         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
244         pf.Pf_Filter [pf.Pf_FilterLen++] = htons (ETHERTYPE_IP);
245         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT;
246         pf.Pf_Filter [pf.Pf_FilterLen++] = htons (IPPROTO_UDP);
247         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 11;
248         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_AND;
249         pf.Pf_Filter [pf.Pf_FilterLen++] = htons (0xFF);
250         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_CAND;
251         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 18;
252         pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
253         pf.Pf_Filter [pf.Pf_FilterLen++] = local_port;
254
255         /* Install the filter... */
256         sio.ic_cmd = NIOCSETF;
257         sio.ic_len = sizeof pf;
258         sio.ic_dp = (char *)&pf;
259         sio.ic_timout = INFTIM;
260         if (ioctl (info -> rfdesc, I_STR, &sio) < 0)
261                 log_fatal ("Can't set NIT filter on %s: %m", info -> name);
262
263         if (!quiet_interface_discovery)
264                 log_info ("Listening on NIT/%s%s%s",
265                       print_hw_addr (info -> hw_address.hbuf [0],
266                                      info -> hw_address.hlen - 1,
267                                      &info -> hw_address.hbuf [1]),
268                       (info -> shared_network ? "/" : ""),
269                       (info -> shared_network ?
270                        info -> shared_network -> name : ""));
271 }
272
273 void if_deregister_receive (info)
274         struct interface_info *info;
275 {
276         /* If we're using the nit API for sending and receiving,
277            we don't need to register this interface twice. */
278         close (info -> rfdesc);
279         info -> rfdesc = -1;
280
281         if (!quiet_interface_discovery)
282                 log_info ("Disabling input on NIT/%s%s%s",
283                       print_hw_addr (info -> hw_address.hbuf [0],
284                                      info -> hw_address.hlen - 1,
285                                      &info -> hw_address.hbuf [1]),
286                       (info -> shared_network ? "/" : ""),
287                       (info -> shared_network ?
288                        info -> shared_network -> name : ""));
289 }
290 #endif /* USE_NIT_RECEIVE */
291
292 #ifdef USE_NIT_SEND
293 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
294         struct interface_info *interface;
295         struct packet *packet;
296         struct dhcp_packet *raw;
297         size_t len;
298         struct in_addr from;
299         struct sockaddr_in *to;
300         struct hardware *hto;
301 {
302         unsigned hbufp, ibufp;
303         double hh [16];
304         double ih [1536 / sizeof (double)];
305         unsigned char *buf = (unsigned char *)ih;
306         struct sockaddr *junk;
307         struct strbuf ctl, data;
308         struct sockaddr_in foo;
309         int result;
310
311         if (!strcmp (interface -> name, "fallback"))
312                 return send_fallback (interface, packet, raw,
313                                       len, from, to, hto);
314
315         /* Start with the sockaddr struct... */
316         junk = (struct sockaddr *)&hh [0];
317         hbufp = (((unsigned char *)&junk -> sa_data [0]) -
318                  (unsigned char *)&hh[0]);
319         ibufp = 0;
320
321         /* Assemble the headers... */
322         assemble_hw_header (interface, (unsigned char *)junk, &hbufp, hto);
323         assemble_udp_ip_header (interface, buf, &ibufp,
324                                 from.s_addr, to -> sin_addr.s_addr,
325                                 to -> sin_port, (unsigned char *)raw, len);
326
327         /* Copy the data into the buffer (yuk). */
328         memcpy (buf + ibufp, raw, len);
329
330         /* Set up the sockaddr structure... */
331 #if USE_SIN_LEN
332         junk -> sa_len = hbufp - 2; /* XXX */
333 #endif
334         junk -> sa_family = AF_UNSPEC;
335
336         /* Set up the msg_buf structure... */
337         ctl.buf = (char *)&hh [0];
338         ctl.maxlen = ctl.len = hbufp;
339         data.buf = (char *)&ih [0];
340         data.maxlen = data.len = ibufp + len;
341
342         result = putmsg (interface -> wfdesc, &ctl, &data, 0);
343         if (result < 0)
344                 log_error ("send_packet: %m");
345         return result;
346 }
347 #endif /* USE_NIT_SEND */
348
349 #ifdef USE_NIT_RECEIVE
350 ssize_t receive_packet (interface, buf, len, from, hfrom)
351         struct interface_info *interface;
352         unsigned char *buf;
353         size_t len;
354         struct sockaddr_in *from;
355         struct hardware *hfrom;
356 {
357         int nread;
358         int length = 0;
359         int offset = 0;
360         unsigned char ibuf [1536];
361         int bufix = 0;
362
363         length = read (interface -> rfdesc, ibuf, sizeof ibuf);
364         if (length <= 0)
365                 return length;
366
367         /* Decode the physical header... */
368         offset = decode_hw_header (interface, ibuf, bufix, hfrom);
369
370         /* If a physical layer checksum failed (dunno of any
371            physical layer that supports this, but WTH), skip this
372            packet. */
373         if (offset < 0) {
374                 return 0;
375         }
376
377         bufix += offset;
378         length -= offset;
379
380         /* Decode the IP and UDP headers... */
381         offset = decode_udp_ip_header (interface, ibuf, bufix,
382                                        from, (unsigned char *)0, length);
383
384         /* If the IP or UDP checksum was bad, skip the packet... */
385         if (offset < 0)
386                 return 0;
387
388         bufix += offset;
389         length -= offset;
390
391         /* Copy out the data in the packet... */
392         memcpy (buf, &ibuf [bufix], length);
393         return length;
394 }
395
396 int can_unicast_without_arp (ip)
397         struct interface_info *ip;
398 {
399         return 1;
400 }
401
402 int can_receive_unicast_unconfigured (ip)
403         struct interface_info *ip;
404 {
405         return 1;
406 }
407
408 int supports_multiple_interfaces (ip)
409         struct interface_info *ip;
410 {
411         return 1;
412 }
413
414 void maybe_setup_fallback ()
415 {
416         isc_result_t status;
417         struct interface_info *fbi = (struct interface_info *)0;
418         if (setup_fallback (&fbi, MDL)) {
419                 if_register_fallback (fbi);
420                 status = omapi_register_io_object ((omapi_object_t *)fbi,
421                                                    if_readsocket, 0,
422                                                    fallback_discard, 0, 0);
423                 if (status != ISC_R_SUCCESS)
424                         log_fatal ("Can't register I/O handle for %s: %s",
425                                    fbi -> name, isc_result_totext (status));
426                 interface_dereference (&fbi, MDL);
427         }
428 }
429 #endif