3 Network Interface Tap (NIT) network interface code, by Ted Lemon
4 with one crucial tidbit of help from Stu Grossmen. */
7 * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-2003 by Internet Software Consortium
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Internet Systems Consortium, Inc.
24 * Redwood City, CA 94063
26 * https://www.isc.org/
28 * This software has been written for Internet Systems Consortium
29 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
30 * To learn more about Internet Systems Consortium, see
31 * ``https://www.isc.org/''. To learn more about Vixie Enterprises,
32 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
33 * ``http://www.nominum.com''.
37 #if defined (USE_NIT_SEND) || defined (USE_NIT_RECEIVE)
38 #include <sys/ioctl.h>
43 #include <net/nit_if.h>
44 #include <net/nit_pf.h>
45 #include <net/nit_buf.h>
46 #include <sys/stropts.h>
47 #include <net/packetfilt.h>
49 #include <netinet/in_systm.h>
50 #include "includes/netinet/ip.h"
51 #include "includes/netinet/udp.h"
52 #include "includes/netinet/if_ether.h"
54 /* Reinitializes the specified interface after an address change. This
55 is not required for packet-filter APIs. */
58 void if_reinitialize_send (info)
59 struct interface_info *info;
64 #ifdef USE_NIT_RECEIVE
65 void if_reinitialize_receive (info)
66 struct interface_info *info;
71 /* Called by get_interface_list for each interface that's discovered.
72 Opens a packet filter for each interface and adds it to the select
75 int if_register_nit (info)
76 struct interface_info *info;
83 /* Open a NIT device */
84 sock = open ("/dev/nit", O_RDWR);
86 log_fatal ("Can't open NIT device for %s: %m", info -> name);
88 /* Set the NIT device to point at this interface. */
89 sio.ic_cmd = NIOCBIND;
90 sio.ic_len = sizeof *(info -> ifp);
91 sio.ic_dp = (char *)(info -> ifp);
92 sio.ic_timout = INFTIM;
93 if (ioctl (sock, I_STR, &sio) < 0)
94 log_fatal ("Can't attach interface %s to nit device: %m",
97 /* Get the low-level address... */
98 sio.ic_cmd = SIOCGIFADDR;
99 sio.ic_len = sizeof ifr;
100 sio.ic_dp = (char *)𝔦
101 sio.ic_timout = INFTIM;
102 if (ioctl (sock, I_STR, &sio) < 0)
103 log_fatal ("Can't get physical layer address for %s: %m",
106 /* XXX code below assumes ethernet interface! */
107 info -> hw_address.hlen = 7;
108 info -> hw_address.hbuf [0] = ARPHRD_ETHER;
109 memcpy (&info -> hw_address.hbuf [1],
110 ifr.ifr_ifru.ifru_addr.sa_data, 6);
112 if (ioctl (sock, I_PUSH, "pf") < 0)
113 log_fatal ("Can't push packet filter onto NIT for %s: %m",
118 #endif /* USE_NIT_SEND || USE_NIT_RECEIVE */
121 void if_register_send (info)
122 struct interface_info *info;
124 /* If we're using the nit API for sending and receiving,
125 we don't need to register this interface twice. */
126 #ifndef USE_NIT_RECEIVE
127 struct packetfilt pf;
130 info -> wfdesc = if_register_nit (info);
134 pf.Pf_Filter [0] = ENF_PUSHZERO;
136 /* Set up an NIT filter that rejects everything... */
137 sio.ic_cmd = NIOCSETF;
138 sio.ic_len = sizeof pf;
139 sio.ic_dp = (char *)&pf;
140 sio.ic_timout = INFTIM;
141 if (ioctl (info -> wfdesc, I_STR, &sio) < 0)
142 log_fatal ("Can't set NIT filter: %m");
144 info -> wfdesc = info -> rfdesc;
146 if (!quiet_interface_discovery)
147 log_info ("Sending on NIT/%s%s%s",
148 print_hw_addr (info -> hw_address.hbuf [0],
149 info -> hw_address.hlen - 1,
150 &info -> hw_address.hbuf [1]),
151 (info -> shared_network ? "/" : ""),
152 (info -> shared_network ?
153 info -> shared_network -> name : ""));
156 void if_deregister_send (info)
157 struct interface_info *info;
159 /* If we're using the nit API for sending and receiving,
160 we don't need to register this interface twice. */
161 #ifndef USE_NIT_RECEIVE
162 close (info -> wfdesc);
165 if (!quiet_interface_discovery)
166 log_info ("Disabling output on NIT/%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_NIT_SEND */
176 #ifdef USE_NIT_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 NIT program! XXX */
181 void if_register_receive (info)
182 struct interface_info *info;
186 struct packetfilt pf;
191 /* Open a NIT device and hang it on this interface... */
192 info -> rfdesc = if_register_nit (info);
194 /* Set the snap length to 0, which means always take the whole
197 if (ioctl (info -> rfdesc, NIOCSSNAP, &x) < 0)
198 log_fatal ("Can't set NIT snap length on %s: %m", info -> name);
200 /* Set the stream to byte stream mode */
201 if (ioctl (info -> rfdesc, I_SRDOPT, RMSGN) != 0)
202 log_info ("I_SRDOPT failed on %s: %m", info -> name);
205 /* Push on the chunker... */
206 if (ioctl (info -> rfdesc, I_PUSH, "nbuf") < 0)
207 log_fatal ("Can't push chunker onto NIT STREAM: %m");
209 /* Set the timeout to zero. */
212 if (ioctl (info -> rfdesc, NIOCSTIME, &t) < 0)
213 log_fatal ("Can't set chunk timeout: %m");
216 /* Ask for no header... */
218 if (ioctl (info -> rfdesc, NIOCSFLAGS, &x) < 0)
219 log_fatal ("Can't set NIT flags on %s: %m", info -> name);
221 /* Set up the NIT filter program. */
222 /* XXX Unlike the BPF filter program, this one won't work if the
223 XXX IP packet is fragmented or if there are options on the IP
228 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 6;
229 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
230 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (ETHERTYPE_IP);
231 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT;
232 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (IPPROTO_UDP);
233 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 11;
234 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_AND;
235 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (0xFF);
236 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_CAND;
237 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 18;
238 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
239 pf.Pf_Filter [pf.Pf_FilterLen++] = local_port;
241 /* Install the filter... */
242 sio.ic_cmd = NIOCSETF;
243 sio.ic_len = sizeof pf;
244 sio.ic_dp = (char *)&pf;
245 sio.ic_timout = INFTIM;
246 if (ioctl (info -> rfdesc, I_STR, &sio) < 0)
247 log_fatal ("Can't set NIT filter on %s: %m", info -> name);
249 if (!quiet_interface_discovery)
250 log_info ("Listening on NIT/%s%s%s",
251 print_hw_addr (info -> hw_address.hbuf [0],
252 info -> hw_address.hlen - 1,
253 &info -> hw_address.hbuf [1]),
254 (info -> shared_network ? "/" : ""),
255 (info -> shared_network ?
256 info -> shared_network -> name : ""));
259 void if_deregister_receive (info)
260 struct interface_info *info;
262 /* If we're using the nit API for sending and receiving,
263 we don't need to register this interface twice. */
264 close (info -> rfdesc);
267 if (!quiet_interface_discovery)
268 log_info ("Disabling input on NIT/%s%s%s",
269 print_hw_addr (info -> hw_address.hbuf [0],
270 info -> hw_address.hlen - 1,
271 &info -> hw_address.hbuf [1]),
272 (info -> shared_network ? "/" : ""),
273 (info -> shared_network ?
274 info -> shared_network -> name : ""));
276 #endif /* USE_NIT_RECEIVE */
279 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
280 struct interface_info *interface;
281 struct packet *packet;
282 struct dhcp_packet *raw;
285 struct sockaddr_in *to;
286 struct hardware *hto;
288 unsigned hbufp, ibufp;
290 double ih [1536 / sizeof (double)];
291 unsigned char *buf = (unsigned char *)ih;
292 struct sockaddr *junk;
293 struct strbuf ctl, data;
294 struct sockaddr_in foo;
297 if (!strcmp (interface -> name, "fallback"))
298 return send_fallback (interface, packet, raw,
301 if (hto == NULL && interface->anycast_mac_addr.hlen)
302 hto = &interface->anycast_mac_addr;
304 /* Start with the sockaddr struct... */
305 junk = (struct sockaddr *)&hh [0];
306 hbufp = (((unsigned char *)&junk -> sa_data [0]) -
307 (unsigned char *)&hh[0]);
310 /* Assemble the headers... */
311 assemble_hw_header (interface, (unsigned char *)junk, &hbufp, hto);
312 assemble_udp_ip_header (interface, buf, &ibufp,
313 from.s_addr, to -> sin_addr.s_addr,
314 to -> sin_port, (unsigned char *)raw, len);
316 /* Copy the data into the buffer (yuk). */
317 memcpy (buf + ibufp, raw, len);
319 /* Set up the sockaddr structure... */
321 junk -> sa_len = hbufp - 2; /* XXX */
323 junk -> sa_family = AF_UNSPEC;
325 /* Set up the msg_buf structure... */
326 ctl.buf = (char *)&hh [0];
327 ctl.maxlen = ctl.len = hbufp;
328 data.buf = (char *)&ih [0];
329 data.maxlen = data.len = ibufp + len;
331 result = putmsg (interface -> wfdesc, &ctl, &data, 0);
333 log_error ("send_packet: %m");
336 #endif /* USE_NIT_SEND */
338 #ifdef USE_NIT_RECEIVE
339 ssize_t receive_packet (interface, buf, len, from, hfrom)
340 struct interface_info *interface;
343 struct sockaddr_in *from;
344 struct hardware *hfrom;
349 unsigned char ibuf [1536];
353 length = read (interface -> rfdesc, ibuf, sizeof ibuf);
357 /* Decode the physical header... */
358 offset = decode_hw_header (interface, ibuf, bufix, hfrom);
360 /* If a physical layer checksum failed (dunno of any
361 physical layer that supports this, but WTH), skip this
370 /* Decode the IP and UDP headers... */
371 offset = decode_udp_ip_header (interface, ibuf, bufix,
372 from, length, &paylen);
374 /* If the IP or UDP checksum was bad, skip the packet... */
382 log_fatal("Internal inconsistency at %s:%d.", MDL);
384 /* Copy out the data in the packet... */
385 memcpy(buf, &ibuf[bufix], paylen);
389 int can_unicast_without_arp (ip)
390 struct interface_info *ip;
395 int can_receive_unicast_unconfigured (ip)
396 struct interface_info *ip;
401 int supports_multiple_interfaces (ip)
402 struct interface_info *ip;
407 void maybe_setup_fallback ()
410 struct interface_info *fbi = (struct interface_info *)0;
411 if (setup_fallback (&fbi, MDL)) {
412 if_register_fallback (fbi);
413 status = omapi_register_io_object ((omapi_object_t *)fbi,
415 fallback_discard, 0, 0);
416 if (status != ISC_R_SUCCESS)
417 log_fatal ("Can't register I/O handle for %s: %s",
418 fbi -> name, isc_result_totext (status));
419 interface_dereference (&fbi, MDL);