]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/nit.c
Go back to the BSD license.
[thirdparty/dhcp.git] / 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-2000 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.28 2000/03/17 03:59:01 mellon Exp $ Copyright (c) 1996-2000 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.jbuf [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 bufp;
303 unsigned char buf [1536 + sizeof (struct sockaddr)];
304 struct sockaddr *junk;
305 struct strbuf ctl, data;
306 int hw_end;
307 struct sockaddr_in foo;
308 int result;
309
310 if (!strcmp (interface -> name, "fallback"))
311 return send_fallback (interface, packet, raw,
312 len, from, to, hto);
313
314 /* Start with the sockaddr struct... */
315 junk = (struct sockaddr *)&buf [0];
316 bufp = ((unsigned char *)&junk -> sa_data [0]) - &buf [0];
317
318 /* Assemble the headers... */
319 assemble_hw_header (interface, buf, &bufp, hto);
320 hw_end = bufp;
321 assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
322 to -> sin_addr.s_addr, to -> sin_port,
323 raw, len);
324
325 /* Copy the data into the buffer (yuk). */
326 memcpy (buf + bufp, raw, len);
327
328 /* Set up the sockaddr structure... */
329 #if USE_SIN_LEN
330 junk -> sa_len = hw_end - 2; /* XXX */
331 #endif
332 junk -> sa_family = AF_UNSPEC;
333
334 #if 0 /* Already done. */
335 memcpy (junk.sa_data, buf, hw_len);
336 #endif
337
338 /* Set up the msg_buf structure... */
339 ctl.buf = (char *)&buf [0];
340 ctl.maxlen = ctl.len = hw_end;
341 data.buf = (char *)&buf [hw_end];
342 data.maxlen = data.len = bufp + len - hw_end;
343
344 result = putmsg (interface -> wfdesc, &ctl, &data, 0);
345 if (result < 0)
346 log_error ("send_packet: %m");
347 return result;
348 }
349 #endif /* USE_NIT_SEND */
350
351 #ifdef USE_NIT_RECEIVE
352 ssize_t receive_packet (interface, buf, len, from, hfrom)
353 struct interface_info *interface;
354 unsigned char *buf;
355 size_t len;
356 struct sockaddr_in *from;
357 struct hardware *hfrom;
358 {
359 int nread;
360 int length = 0;
361 int offset = 0;
362 unsigned char ibuf [1536];
363 int bufix = 0;
364
365 length = read (interface -> rfdesc, ibuf, sizeof ibuf);
366 if (length <= 0)
367 return length;
368
369 /* Decode the physical header... */
370 offset = decode_hw_header (interface, ibuf, bufix, hfrom);
371
372 /* If a physical layer checksum failed (dunno of any
373 physical layer that supports this, but WTH), skip this
374 packet. */
375 if (offset < 0) {
376 return 0;
377 }
378
379 bufix += offset;
380 length -= offset;
381
382 /* Decode the IP and UDP headers... */
383 offset = decode_udp_ip_header (interface, ibuf, bufix,
384 from, (unsigned char *)0, length);
385
386 /* If the IP or UDP checksum was bad, skip the packet... */
387 if (offset < 0)
388 return 0;
389
390 bufix += offset;
391 length -= offset;
392
393 /* Copy out the data in the packet... */
394 memcpy (buf, &ibuf [bufix], length);
395 return length;
396 }
397
398 int can_unicast_without_arp (ip)
399 struct interface_info *ip;
400 {
401 return 1;
402 }
403
404 int can_receive_unicast_unconfigured (ip)
405 struct interface_info *ip;
406 {
407 return 1;
408 }
409
410 void maybe_setup_fallback ()
411 {
412 isc_result_t status;
413 struct interface_info *fbi;
414 fbi = setup_fallback ();
415 if (fbi) {
416 if_register_fallback (fbi);
417 fbi -> refcnt = 1;
418 fbi -> type = dhcp_type_interface;
419 status = omapi_register_io_object ((omapi_object_t *)fbi,
420 if_readsocket, 0,
421 fallback_discard, 0, 0);
422 if (status != ISC_R_SUCCESS)
423 log_fatal ("Can't register I/O handle for %s: %s",
424 fbi -> name, isc_result_totext (status));
425 }
426 }
427 #endif