NetBurner 3.1
netinterface.h
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
5 #ifndef _NB_NETIF_H
6 #define _NB_NETIF_H
7 
8 // NB Definitions
9 #include <predef.h>
10 
11 // NB Libs
12 #include <autoip.h>
13 #include <config_netobj.h>
14 #include <config_obj.h>
15 #include <dhcpclient.h>
16 #include <discoveryservlet.h>
17 #include <nettypes.h>
18 /*
19  ******************************************************************************
20  *
21  * Definitions
22  *
23  ******************************************************************************
24  */
25 /*
26  PPP Interface Identifier
27 */
28 #define ARP_PPP_INTERFACE (255)
29 
30 /*
31  ******************************************************************************
32  *
33  * MAC Address for second and subsequent interfaces
34  * For a second and subsequent NICs the MSB(it) of the 5th octet is XOR'ed
35  * with 1 to support prior released tools.
36  *
37  * 6 bytes
38  * <--------------------------------------------------------------------->
39  * 0 1 2 3 4 5
40  * -----------------------------------------------------------------------
41  * | 6th byte | 5th byte | 4th byte | 3rd byte | 2nd byte | 1st byte |
42  * | 1st octet | 2nd octet | 3rd octet | 4th octet | 5th octet | 6th octet |
43  * -----------------------------------------------------------------------
44  *
45  * 3 bytes 3 bytes
46  * <---------------------------------> <--------------------------------->
47  * -----------------------------------------------------------------------
48  * | Organizationally Unique Identifier | Network Interface Controller |
49  * | | (OUI) | | | (NIC) | |
50  * | 0x00 | 0x03 | 0xF4 | 0xYX | 0xZX | 0xXX |
51  * -----------------------------------------------------------------------
52  * | \ OUI is NetBurner's Y is IIXX XXXX binary
53  * | \ I is the interface defined as
54  * | \ bits in NB_MAX_NIC... below
55  * | | Z is WXXX binary
56  * V V W ix XOR'd bit
57  * |1|2|3|4|5|6|7|8| (7) 0 global, 1 local, (8) 0 unicast, 1 multicast (IEE)
58  *
59  * Notes:
60  * 0-5 are C language offsets, 6th byte through 1st byte is Ethernet
61  * transmission order and octet notation is from IEEE 802 MAC-48 address.
62  * Derivation is needed if the NIC does not have a non-volatile MAC address.
63  *
64  ******************************************************************************
65  */
66 #ifndef NB_MAC_ADDRESS
67 #define NB_MAC_ADDRESS
68 
69 #include <buffers.h>
70 
71 /* 4th Octet */
72 #define NB_MAC_NIC_INTERFACE (0x00)
73 #define NB_MAC_NIC_SECOND_INTERFACE (0x80)
74 #define NB_MAC_NIC_THIRD_INTERFACE (0x40)
75 #define NB_MAC_NIC_FOURTH_INTERFACE (0xC0)
76 #define NB_MAC_NIC_SECOND_INTERFACE_MASK \
77  (NB_MAC_NIC_INTERFACE | NB_MAC_NIC_SECOND_INTERFACE | NB_MAC_NIC_THIRD_INTERFACE | NB_MAC_NIC_FOURTH_INTERFACE)
78 #define NB_MAC_OCTET_4 (3)
79 #define NB_MAC_OCTET_5 (4)
80 #define NB_MAC_OCTET_5_XOR (0x80)
81 
82 #endif /* NB_MAC_ADDRESS */
83 
84 /*
85  ******************************************************************************
86  *
87  * Callback Function Prototypes
88  *
89  ******************************************************************************
90  */
91 
92 /*
93  ******************************************************************************
94 
95  Process Internet Protocol Packet (IP)
96 
97  Parameters:
98  poolPtr - Packet from pool
99  ethernetFramePtr - Ethernet frame
100  checksum - IP frame checksum
101 
102  Return:
103  None
104 
105  Notes;
106  Called for every IP packet received and meets this criteria.
107  1)The packet is an IP packet.
108  2)The IP header checksum is valid.
109  3)The packet was addressed to us or to a broadcast address.
110  Before the callback is called the flags member of the buffer is setup to
111  indicate physical broadcast etc....
112 
113  ******************************************************************************
114  */
115 typedef void(ProcessPacketFunc)(PoolPtr poolPtr, PEFRAME ethernetFramePtr, uint16_t checksum);
116 
117 /*
118  ******************************************************************************
119 
120  Process Address Resolution Packet (ARP)
121 
122  Parameters:
123  poolPtr - Packet from pool
124  ethernetFramePtr - Ethernet frame
125 
126  Return:
127  None
128 
129  Notes;
130  Called for every ARP packet received. Before the callback is called the
131  flags member of the buffer is setup to indicate physical broadcast etc...
132 
133  ******************************************************************************
134  */
135 typedef void(ProcessArpFunc)(PoolPtr poolPtr, PEFRAME ethernetFramePtr);
136 
137 /*
138  ******************************************************************************
139 
140  Returns current link active status
141 
142  Parameters:
143  None
144 
145  Return:
146  TRUE active, FALSE all else.
147 
148  Notes:
149  Interface driver must minimize latency.
150 
151  ******************************************************************************
152  */
153 typedef BOOL(LinkActiveFunc)(void);
154 
155 /*
156  ******************************************************************************
157  *
158  * Data Structures
159  *
160  ******************************************************************************
161  */
162 
163 #define INTF_CSUM_IP (0x01)
164 #define INTF_CSUM_ICMP (0x02)
165 #define INTF_CSUM_UDP (0x04)
166 #define INTF_CSUM_TCP (0x08)
167 #define INTF_CSUM_IP6 (0x10)
168 #define INTF_CSUM_ICMP6 (0x20)
169 #define INTF_CSUM_UDP6 (0x40)
170 #define INTF_CSUM_TCP6 (0x80)
171 
172 #define INTF_CSUM_ALL4 (INTF_CSUM_IP | INTF_CSUM_ICMP | INTF_CSUM_UDP | INTF_CSUM_TCP)
173 #define INTF_CSUM_ALL6 (INTF_CSUM_IP6 | INTF_CSUM_ICMP6 | INTF_CSUM_UDP6 | INTF_CSUM_TCP6)
174 /*
175  ******************************************************************************
176 
177  Interface Configuration and Control Block
178 
179  Ethernet
180  theMac - Interface ethernet MAC address
181 
182  TCP/IP
183  netIP - Interface IP address
184  netIpMask - Interface network IP mask
185  netIpGate - Interface gateway IP address
186  netMss - Interface maximum segment size (0 default)
187 
188  Network Processing
189  send_func - Transmit packet function
190  kill_if - Kill interface function
191  enab_multicast - Enable/Disable multicast addresses
192 
193  Identification
194  InterfaceName - Name
195  config_num - Configuration record number
196 
197  Multi-Home Feature
198  bMultiHome - Multi-home?
199  root_if - Physical interface number
200 
201  Routing Support
202  LinkActiveFuncPtr - Link active function
203 
204  Accelerator Support
205  checksumOffload - Bitfield indicating TX checksums the
206  interface automatically generates
207 
208  Notes:
209  Should not change data sizes and always add to the end to avoid breaking
210  use for interface configuration.
211 
212  ******************************************************************************
213  */
214 class InterfaceBlock : public config_obj
215 {
216  public:
217  I4Record ip4{"IPv4"}; // The IPv4 configuration for "this" interface, see config_netobj.h for details
218 #ifdef IPV6
219  I6Record ip6{"IPv6"}; // The IPv6 configuration for "this" interface, see config_netobj.h for details
220 #endif
221  config_MACADR MAC{"00:00:00:00:00:00", "MAC"}; // Interface MAC address. May be 0/null for interfaces such as PPP
222  config_string device_name{"", "DeviceName"}; // Name of interface. Used to register for DNS and NetBIOS
223  config_int dhcp_discover_secs{1, "DhcpDiscoverSec"}; // How long to wait after boot before sending a DHCP Discover message
224  config_string discovery_server{"discover.netburner.com/DevicePost", "DiscoveryReportUrl"}; // NetBurner discover server name
225  config_int discovery_interval{(15 * 60), "DiscoveryReportInterval"}; // How often to report to NetBurner discover server
226  config_bool obfuscate_discovery{true,"DiscoveryObfuscate","Should discovery obfuscate data"};
227  config_int vlan_tag_value{0,"VlanTag"};
228 
229  ConfigEndMarker; // Marker for end of configuration structure
230 
231  int32_t root_if; // Root interface number. If multihomed, the interface would be a child of the root interface
232  int32_t my_ifnum; // This interface number
233  uint16_t netMss; // Network max segment size for this interface
234  uint8_t checksumOffload; // Non-zero if hardware interface support offloading checksum calculation
235  bool bHaveSeenLink; // Have seen a link at once before
236 
237  DhcpObject dhcpClient{this}; // DHCP client support
238  discover_servlet disco_servlet{this}; // the servlet that runs the NetBurner discovery server process
239 
240 #ifdef AUTOIP // if not defined don't compile anything...
241  AutoIPClient AutoClient; // AutoIP client object for this interface
242 #endif
243 #if defined MULTIHOME
244  BOOL bMultiHome; // True if this is a multihomed interface
245  MultiInterfaceBlock *pChild; // Multihomed child interface
246 #endif
247 
248  InterfaceBlock(const char *name, const char *desc = NULL);
249  InterfaceBlock(config_obj &owner, const char *name, const char *desc = NULL);
250  void RegisterInterface();
251 
252  virtual void send_func(PoolPtr poolPtr) = 0;
253  virtual void kill_if();
254  virtual void EnableMulticast(MACADR macAddress, BOOL addAddress) = 0;
255  virtual bool LinkActive() = 0;
256  virtual int LinkSpeed() = 0;
257  virtual bool LinkDuplex() = 0;
258  void InterfaceLinkChange(bool link);
259  virtual const char *GetInterfaceName();
260 
261  virtual bool bNeedsArp() { return true; };
262  void fdShowInterfaceValues(int fd);
263  inline void ShowInterfaceValues() { fdShowInterfaceValues(1); };
264  void SetDefaultFlags();
265  // Called by DHCP client when we get new settings...
266  void DHCPNotify();
267 
268 #if defined MULTIHOME
269  int RegisterMultiHomeInterface(MultiInterfaceBlock &InterfaceBlock);
270 #endif
271 };
272 
273 // Adds additional features to an InterfaceBlock. Defaults to most features disabled
274 class EtherLikeInterface : public InterfaceBlock
275 {
276  public:
277  ConfigEndMarker;
278  EtherLikeInterface(const char *name, const char *desc = NULL) : InterfaceBlock(name, desc)
279  {
280  ip4.SetFlag(fConfigHidden);
281 #ifdef IPV6
282  ip6.SetFlag(fConfigHidden);
283 #endif
284  MAC.SetFlag(fConfigHidden);
285  SetFlag(fConfigHidden);
286  }
287 
288  EtherLikeInterface(config_obj &owner, const char *name, const char *desc = NULL) : InterfaceBlock(owner, name, desc)
289  {
290  ip4.SetFlag(fConfigHidden);
291 #ifdef IPV6
292  ip6.SetFlag(fConfigHidden);
293 #endif
294  MAC.SetFlag(fConfigHidden);
295  SetFlag(fConfigHidden);
296  }
297 
298  virtual void Enable()
299  {
300  ClrFlag(fConfigHidden);
301  ip4.ClrFlag(fConfigHidden);
302 #ifdef IPV6
303  ip6.ClrFlag(fConfigHidden);
304 #endif
305  MAC.ClrFlag(fConfigHidden);
306  MAC.SetFlag(fConfigReadOnly);
307  }
308 };
309 
310 /* Global receive processing definition */
311 int NetDoRX(PoolPtr pp, uint16_t count, int if_num);
312 
313 /*
314  ******************************************************************************
315  *
316  * External Definitions
317  *
318  ******************************************************************************
319  */
320 /*
321  System ARP Process Function
322 */
323 extern ProcessArpFunc *pArpFunc;
324 
325 #ifdef IPV6
326 
327 /*
328  ******************************************************************************
329 
330  IP6 Network Packet
331 
332  Parameters:
333  poolPtr - Packet from pool
334 
335  Return:
336  None
337 
338  ******************************************************************************
339  */
340 typedef void(ProcessIp6Func)(PoolPtr poolPtr);
341 
342 extern ProcessIp6Func *pIp6Func;
343 
344 #endif
345 
346 /*
347  System IP Packet Process Function
348 */
349 extern ProcessPacketFunc *pPacketfunc;
350 
351 /*
352  ******************************************************************************
353  *
354  * Routine Prototypes
355  *
356  ******************************************************************************
357  */
358 
359 /*
360  ******************************************************************************
361 
362  Selects Packet Transmission for the Interface
363 
364  Parameters:
365  poolPtr - Packet from pool
366  interface - Interface number
367 
368 
369  Return:
370  None
371 
372  Notes:
373  Frees buffer if interface is not valid
374 
375  ******************************************************************************
376  */
377 void TransmitBuffer(PoolPtr poolPtr, int interface);
378 
379 /*
380  ******************************************************************************
381 
382  Extracts Data (Ethernet Frame) Pointer from Pool Packet
383 
384  Parameters:
385  poolPtr - Packet from pool
386 
387 
388  Return:
389  Pointer to data segment (ethernet frame) of pool packet.
390 
391  Notes:
392  Does not check for validity
393 
394  ******************************************************************************
395  */
396 PEFRAME GetEframe(PoolPtr poolPtr);
397 
398 /*
399  ******************************************************************************
400 
401  Register interface with network system.
402 
403  Parameters:
404  interfaceBlockPtr - Interface block
405 
406  Return:
407  Interface number, 0 if exceeded interfaces allowed (MAX_INTERFACES)
408 
409  Notes:
410  RegisterMultiHomeInterface for Multihome interface use.
411 
412  ******************************************************************************
413  */
414 
415 /*
416  ******************************************************************************
417 
418  Remove interface with network system.
419 
420  Parameters:
421  interface - Interface number
422 
423  Return:
424  1 removed, 0 if otherwise invalid or empty
425 
426  Notes:
427  None
428 
429  ******************************************************************************
430  */
431 int Removeinterface(int interface);
432 
433 /*
434  ******************************************************************************
435 
436  Enable/Disable Multicast on an interface.
437 
438  Parameters:
439  macAddress - Multicast MAC address
440  interface - Interface number
441 
442  Return:
443  None
444 
445  Notes:
446  Calls registered interface multicast routine.
447 
448  ******************************************************************************
449  */
450 void EnableMulticast(MACADR macAddress, int interface = 0);
451 void DisableMulticast(MACADR macAddress, int interface = 0);
452 
453 /*
454  ******************************************************************************
455 
456  Get interface block
457 
458  Parameters:
459  interface - Interface number
460 
461  Return:
462  Interface block, NULL if not valid interface
463 
464  Notes:
465  None
466 
467  ******************************************************************************
468  */
469 InterfaceBlock *GetInterfaceBlock(int interface = 0);
470 
471 /*
472  ******************************************************************************
473 
474  Get first and subsequent interface numbers
475 
476  Parameters:
477  lastInterface - Last valid interface number
478 
479  Return:
480  Next valid interface number, 0 if exhausted
481 
482  Notes:
483  None
484 
485  ******************************************************************************
486  */
487 int32_t GetFirstInterface(void);
488 int32_t GetNextInterface(int lastInterface);
489 int32_t GetInterfaceNumber(InterfaceBlock *pifb);
490 int32_t GetInterfaceForMyAddress4(IPADDR4 ip);
491 bool GetInterfaceLink(int ifn);
492 
493 /*
494  ******************************************************************************
495  *
496  * Runtime Libraries Routine Prototypes
497  *
498  ******************************************************************************
499  */
500 
501 /*
502  ******************************************************************************
503 
504  Initialize the first Ethernet Interface
505 
506  Parameters:
507  processPacketFuncPtr - IP Packet process function
508  processArpFuncPtr - ARP Packet process function
509 
510 
511  Return:
512  TRUE success, else problems
513 
514  Notes:
515  Should be called once in the system to create first interface.
516 
517  ******************************************************************************
518  */
519 BOOL InitializeNetwork(ProcessPacketFunc *processPacketFuncPtr, ProcessArpFunc *processArpFuncPtr);
520 
521 /*
522  ******************************************************************************
523 
524  Disables network.
525 
526  Parameters:
527  None
528 
529  Return:
530  None
531 
532  Notes:
533  None
534 
535  ******************************************************************************
536  */
537 void StopNetworks(void);
538 
539 /*
540  ******************************************************************************
541 
542  Get current network settings.
543 
544  Parameters:
545  interface - Interface number
546 
547  Return:
548  Specific setting requested for the interface, 0 if invalid interface.
549 
550  Notes:
551  None
552 
553  ******************************************************************************
554  */
555 MACADR InterfaceMAC(int interface);
556 IPADDR4 InterfaceIP(int interface);
557 IPADDR4 InterfaceAutoIP(int interface);
558 IPADDR4 InterfaceDNS(int interface);
559 IPADDR4 InterfaceDNS2(int interface);
560 IPADDR4 InterfaceMASK(int interface);
561 IPADDR4 InterfaceGate(int interface);
562 bool InterfaceLinkActive(int interface);
563 int InterfaceLinkSpeed(int interface);
564 bool InterfaceLinkDuplex(int interface);
565 const char *InterfaceName(int interface);
566 
567 #endif /* _NB_NETIF_H */
Definition: config_obj.h:134
Configuration object header file.
#define NULL
Definition: nm_bsp.h:76
NetBurner Buffers API.