1.\" Copyright (c) 1996-1999 Whistle Communications, Inc. 2.\" All rights reserved. 3.\" 4.\" Subject to the following obligations and disclaimer of warranty, use and 5.\" redistribution of this software, in source or object code forms, with or 6.\" without modifications are expressly permitted by Whistle Communications; 7.\" provided, however, that: 8.\" 1. Any and all reproductions of the source or object code must include the 9.\" copyright notice above and the following disclaimer of warranties; and 10.\" 2. No rights are granted, in any manner or form, to use Whistle 11.\" Communications, Inc. trademarks, including the mark "WHISTLE 12.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as 13.\" such appears in the above copyright notice or in the software. 14.\" 15.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 16.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 17.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 18.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 20.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 21.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 22.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 23.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 24.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 25.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 26.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 27.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 31.\" OF SUCH DAMAGE. 32.\" 33.\" Authors: Julian Elischer <julian@FreeBSD.org> 34.\" Archie Cobbs <archie@FreeBSD.org> 35.\" 36.\" $FreeBSD: src/share/man/man4/netgraph.4,v 1.39.2.1 2001/12/21 09:00:50 ru Exp $ 37.\" $Whistle: netgraph.4,v 1.7 1999/01/28 23:54:52 julian Exp $ 38.\" 39.Dd September 2, 2008 40.Dt NETGRAPH 4 41.Os 42.Sh NAME 43.Nm netgraph 44.Nd graph based kernel networking subsystem 45.Sh DESCRIPTION 46The 47.Nm 48system provides a uniform and modular system for the implementation 49of kernel objects which perform various networking functions. The objects, 50known as 51.Em nodes , 52can be arranged into arbitrarily complicated graphs. Nodes have 53.Em hooks 54which are used to connect two nodes together, forming the edges in the graph. 55Nodes communicate along the edges to process data, implement protocols, etc. 56.Pp 57The aim of 58.Nm 59is to supplement rather than replace the existing kernel networking 60infrastructure. It provides: 61.Pp 62.Bl -bullet -compact -offset 2n 63.It 64A flexible way of combining protocol and link level drivers 65.It 66A modular way to implement new protocols 67.It 68A common framework for kernel entities to inter-communicate 69.It 70A reasonably fast, kernel-based implementation 71.El 72.Sh Nodes and Types 73The most fundamental concept in 74.Nm 75is that of a 76.Em node . 77All nodes implement a number of predefined methods which allow them 78to interact with other nodes in a well defined manner. 79.Pp 80Each node has a 81.Em type , 82which is a static property of the node determined at node creation time. 83A node's type is described by a unique 84.Tn ASCII 85type name. 86The type implies what the node does and how it may be connected 87to other nodes. 88.Pp 89In object-oriented language, types are classes and nodes are instances 90of their respective class. All node types are subclasses of the generic node 91type, and hence inherit certain common functionality and capabilities 92(e.g., the ability to have an 93.Tn ASCII 94name). 95.Pp 96Nodes may be assigned a globally unique 97.Tn ASCII 98name which can be 99used to refer to the node. 100The name must not contain the characters 101.Dq .\& 102or 103.Dq \&: 104and is limited to 105.Dv "NG_NODESIZ" 106characters (including NUL byte). 107.Pp 108Each node instance has a unique 109.Em ID number 110which is expressed as a 32-bit hex value. This value may be used to 111refer to a node when there is no 112.Tn ASCII 113name assigned to it. 114.Sh Hooks 115Nodes are connected to other nodes by connecting a pair of 116.Em hooks , 117one from each node. Data flows bidirectionally between nodes along 118connected pairs of hooks. A node may have as many hooks as it 119needs, and may assign whatever meaning it wants to a hook. 120.Pp 121Hooks have these properties: 122.Pp 123.Bl -bullet -compact -offset 2n 124.It 125A hook has an 126.Tn ASCII 127name which is unique among all hooks 128on that node (other hooks on other nodes may have the same name). 129The name must not contain a 130.Dq .\& 131or a 132.Dq \&: 133and is 134limited to 135.Dv "NG_HOOKSIZ" 136characters (including NUL byte). 137.It 138A hook is always connected to another hook. That is, hooks are 139created at the time they are connected, and breaking an edge by 140removing either hook destroys both hooks. 141.El 142.Pp 143A node may decide to assign special meaning to some hooks. 144For example, connecting to the hook named 145.Dq debug 146might trigger 147the node to start sending debugging information to that hook. 148.Sh Data Flow 149Two types of information flow between nodes: data messages and 150control messages. Data messages are passed in mbuf chains along the edges 151in the graph, one edge at a time. The first mbuf in a chain must have the 152.Dv M_PKTHDR 153flag set. Each node decides how to handle data coming in on its hooks. 154.Pp 155Control messages are type-specific C structures sent from one node 156directly to some arbitrary other node. Control messages have a common 157header format, followed by type-specific data, and are binary structures 158for efficiency. However, node types also may support conversion of the 159type specific data between binary and 160.Tn ASCII 161for debugging and human interface purposes (see the 162.Dv NGM_ASCII2BINARY 163and 164.Dv NGM_BINARY2ASCII 165generic control messages below). Nodes are not required to support 166these conversions. 167.Pp 168There are two ways to address a control message. If 169there is a sequence of edges connecting the two nodes, the message 170may be 171.Dq source routed 172by specifying the corresponding sequence 173of hooks as the destination address for the message (relative 174addressing). Otherwise, the recipient node global 175.Tn ASCII 176name 177(or equivalent ID based name) is used as the destination address 178for the message (absolute addressing). The two types of addressing 179may be combined, by specifying an absolute start node and a sequence 180of hooks. 181.Pp 182Messages often represent commands that are followed by a reply message 183in the reverse direction. To facilitate this, the recipient of a 184control message is supplied with a 185.Dq return address 186that is suitable for addressing a reply. 187.Pp 188Each control message contains a 32 bit value called a 189.Em typecookie 190indicating the type of the message, i.e., how to interpret it. 191Typically each type defines a unique typecookie for the messages 192that it understands. However, a node may choose to recognize and 193implement more than one type of message. 194.Sh Netgraph is Functional 195In order to minimize latency, most 196.Nm 197operations are functional. 198That is, data and control messages are delivered by making function 199calls rather than by using queues and mailboxes. For example, if node 200A wishes to send a data mbuf to neighboring node B, it calls the 201generic 202.Nm 203data delivery function. This function in turn locates 204node B and calls B's 205.Dq receive data 206method. While this mode of operation 207results in good performance, it has a few implications for node 208developers: 209.Pp 210.Bl -bullet -compact -offset 2n 211.It 212Whenever a node delivers a data or control message, the node 213may need to allow for the possibility of receiving a returning 214message before the original delivery function call returns. 215.It 216Netgraph nodes and support routines generally run inside critical 217sections. 218However, some nodes may want to send data and control messages 219from a different priority level. Netgraph supplies queueing routines which 220utilize the NETISR system to move message delivery inside a critical 221section. 222Note that messages are always received from inside a critical section. 223.It 224It's possible for an infinite loop to occur if the graph contains cycles. 225.El 226.Pp 227So far, these issues have not proven problematical in practice. 228.Sh Interaction With Other Parts of the Kernel 229A node may have a hidden interaction with other components of the 230kernel outside of the 231.Nm 232subsystem, such as device hardware, 233kernel protocol stacks, etc. In fact, one of the benefits of 234.Nm 235is the ability to join disparate kernel networking entities together in a 236consistent communication framework. 237.Pp 238An example is the node type 239.Em socket 240which is both a netgraph node and a 241.Xr socket 2 242.Bx 243socket in the protocol family 244.Dv PF_NETGRAPH . 245Socket nodes allow user processes to participate in 246.Nm . 247Other nodes communicate with socket nodes using the usual methods, and the 248node hides the fact that it is also passing information to and from a 249cooperating user process. 250.Pp 251Another example is a device driver that presents 252a node interface to the hardware. 253.Sh Node Methods 254Nodes are notified of the following actions via function calls 255to the following node methods (all from inside critical sections) 256and may accept or reject that action (by returning the appropriate 257error code): 258.Bl -tag -width xxx 259.It Creation of a new node 260The constructor for the type is called. If creation of a new node is 261allowed, the constructor must call the generic node creation 262function (in object-oriented terms, the superclass constructor) 263and then allocate any special resources it needs. For nodes that 264correspond to hardware, this is typically done during the device 265attach routine. Often a global 266.Tn ASCII 267name corresponding to the 268device name is assigned here as well. 269.It Creation of a new hook 270The hook is created and tentatively 271linked to the node, and the node is told about the name that will be 272used to describe this hook. The node sets up any special data structures 273it needs, or may reject the connection, based on the name of the hook. 274.It Successful connection of two hooks 275After both ends have accepted their 276hooks, and the links have been made, the nodes get a chance to 277find out who their peer is across the link and can then decide to reject 278the connection. Tear-down is automatic. 279.It Destruction of a hook 280The node is notified of a broken connection. The node may consider some hooks 281to be critical to operation and others to be expendable: the disconnection 282of one hook may be an acceptable event while for another it 283may affect a total shutdown for the node. 284.It Shutdown of a node 285This method allows a node to clean up 286and to ensure that any actions that need to be performed 287at this time are taken. The method must call the generic (i.e., superclass) 288node destructor to get rid of the generic components of the node. 289Some nodes (usually associated with a piece of hardware) may be 290.Em persistent 291in that a shutdown breaks all edges and resets the node, 292but doesn't remove it, in which case the generic destructor is not called. 293.El 294.Sh Sending and Receiving Data 295Three other methods are also supported by all nodes: 296.Bl -tag -width xxx 297.It Receive data message 298An mbuf chain is passed to the node. 299The node is notified on which hook the data arrived, 300and can use this information in its processing decision. 301The node must always 302.Fn m_freem 303the mbuf chain on completion or error, or pass it on to another node 304(or kernel module) which will then be responsible for freeing it. 305.Pp 306In addition to the mbuf chain itself there is also a pointer to a 307structure describing meta-data about the message 308(e.g. priority information). This pointer may be 309.Dv NULL 310if there is no additional information. The format for this information is 311described in 312.In netgraph/netgraph.h . 313The memory for meta-data must allocated via 314.Fn malloc 315with type 316.Dv M_NETGRAPH . 317As with the data itself, it is the receiver's responsibility to 318.Fn free 319the meta-data. If the mbuf chain is freed the meta-data must 320be freed at the same time. If the meta-data is freed but the 321real data on is passed on, then a 322.Dv NULL 323pointer must be substituted. 324.Pp 325The receiving node may decide to defer the data by queueing it in the 326.Nm 327NETISR system (see below). 328.Pp 329The structure and use of meta-data is still experimental, but is 330presently used in frame-relay to indicate that management packets 331should be queued for transmission 332at a higher priority than data packets. This is required for 333conformance with Frame Relay standards. 334.Pp 335.It Receive queued data message 336Usually this will be the same function as 337.Em Receive data message. 338This is the entry point called when a data message is being handed to 339the node after having been queued in the NETISR system. 340This allows a node to decide in the 341.Em Receive data message 342method that a message should be deferred and queued, 343and be sure that when it is processed from the queue, 344it will not be queued again. 345.It Receive control message 346This method is called when a control message is addressed to the node. 347A return address is always supplied, giving the address of the node 348that originated the message so a reply message can be sent anytime later. 349.Pp 350It is possible for a synchronous reply to be made, and in fact this 351is more common in practice. 352This is done by setting a pointer (supplied as an extra function parameter) 353to point to the reply. 354Then when the control message delivery function returns, 355the caller can check if this pointer has been made non-NULL, 356and if so then it points to the reply message allocated via 357.Fn malloc 358and containing the synchronous response. In both directions, 359(request and response) it is up to the 360receiver of that message to 361.Fn free 362the control message buffer. All control messages and replies are 363allocated with 364.Fn malloc 365type 366.Dv M_NETGRAPH . 367.El 368.Pp 369Much use has been made of reference counts, so that nodes being 370free'd of all references are automatically freed, and this behaviour 371has been tested and debugged to present a consistent and trustworthy 372framework for the 373.Dq type module 374writer to use. 375.Sh Addressing 376The 377.Nm 378framework provides an unambiguous and simple to use method of specifically 379addressing any single node in the graph. The naming of a node is 380independent of its type, in that another node, or external component 381need not know anything about the node's type in order to address it so as 382to send it a generic message type. Node and hook names should be 383chosen so as to make addresses meaningful. 384.Pp 385Addresses are either absolute or relative. An absolute address begins 386with a node name, (or ID), followed by a colon, followed by a sequence of hook 387names separated by periods. This addresses the node reached by starting 388at the named node and following the specified sequence of hooks. 389A relative address includes only the sequence of hook names, implicitly 390starting hook traversal at the local node. 391.Pp 392There are a couple of special possibilities for the node name. 393The name 394.Dq .\& 395(referred to as 396.Dq \&.: ) 397always refers to the local node. 398Also, nodes that have no global name may be addressed by their ID numbers, 399by enclosing the hex representation of the ID number within square brackets. 400Here are some examples of valid netgraph addresses: 401.Bd -literal -offset 4n -compact 402 403 .: 404 foo: 405 .:hook1 406 foo:hook1.hook2 407 [f057cd80]:hook1 408.Ed 409.Pp 410Consider the following set of nodes might be created for a site with 411a single physical frame relay line having two active logical DLCI channels, 412with RFC 1490 frames on DLCI 16 and PPP frames over DLCI 20: 413.Bd -literal 414[type SYNC ] [type FRAME] [type RFC1490] 415[ "Frame1" ](uplink)<-->(data)[<un-named>](dlci16)<-->(mux)[<un-named> ] 416[ A ] [ B ](dlci20)<---+ [ C ] 417 | 418 | [ type PPP ] 419 +>(mux)[<un-named>] 420 [ D ] 421.Ed 422.Pp 423One could always send a control message to node C from anywhere 424by using the name 425.Em "Frame1:uplink.dlci16" . 426Similarly, 427.Em "Frame1:uplink.dlci20" 428could reliably be used to reach node D, and node A could refer 429to node B as 430.Em ".:uplink" , 431or simply 432.Em "uplink" . 433Conversely, B can refer to A as 434.Em "data" . 435The address 436.Em "mux.data" 437could be used by both nodes C and D to address a message to node A. 438.Pp 439Note that this is only for 440.Em control messages . 441Data messages are routed one hop at a time, by specifying the departing 442hook, with each node making the next routing decision. So when B 443receives a frame on hook 444.Em data 445it decodes the frame relay header to determine the DLCI, 446and then forwards the unwrapped frame to either C or D. 447.Sh Netgraph Structures 448Interesting members of the node and hook structures are shown below: 449.Bd -literal 450struct ng_node { 451 char *name; /* Optional globally unique name */ 452 void *private; /* Node implementation private info */ 453 struct ng_type *type; /* The type of this node */ 454 int refs; /* Number of references to this struct */ 455 int numhooks; /* Number of connected hooks */ 456 hook_p hooks; /* Linked list of (connected) hooks */ 457}; 458typedef struct ng_node *node_p; 459 460struct ng_hook { 461 char *name; /* This node's name for this hook */ 462 void *private; /* Node implementation private info */ 463 int refs; /* Number of references to this struct */ 464 struct ng_node *node; /* The node this hook is attached to */ 465 struct ng_hook *peer; /* The other hook in this connected pair */ 466 struct ng_hook *next; /* Next in list of hooks for this node */ 467}; 468typedef struct ng_hook *hook_p; 469.Ed 470.Pp 471The maintenance of the name pointers, reference counts, and linked list 472of hooks for each node is handled automatically by the 473.Nm 474subsystem. 475Typically a node's private info contains a back-pointer to the node or hook 476structure, which counts as a new reference that must be registered by 477incrementing 478.Dv "node->refs" . 479.Pp 480From a hook you can obtain the corresponding node, and from 481a node the list of all active hooks. 482.Pp 483Node types are described by these structures: 484.Bd -literal 485/** How to convert a control message from binary <-> ASCII */ 486struct ng_cmdlist { 487 u_int32_t cookie; /* typecookie */ 488 int cmd; /* command number */ 489 const char *name; /* command name */ 490 const struct ng_parse_type *mesgType; /* args if !NGF_RESP */ 491 const struct ng_parse_type *respType; /* args if NGF_RESP */ 492}; 493 494struct ng_type { 495 u_int32_t version; /* Must equal NG_VERSION */ 496 const char *name; /* Unique type name */ 497 498 /* Module event handler */ 499 modeventhand_t mod_event; /* Handle load/unload (optional) */ 500 501 /* Constructor */ 502 int (*constructor)(node_p *node); /* Create a new node */ 503 504 /** Methods using the node **/ 505 int (*rcvmsg)(node_p node, /* Receive control message */ 506 struct ng_mesg *msg, /* The message */ 507 const char *retaddr, /* Return address */ 508 struct ng_mesg **resp); /* Synchronous response */ 509 int (*shutdown)(node_p node); /* Shutdown this node */ 510 int (*newhook)(node_p node, /* create a new hook */ 511 hook_p hook, /* Pre-allocated struct */ 512 const char *name); /* Name for new hook */ 513 514 /** Methods using the hook **/ 515 int (*connect)(hook_p hook); /* Confirm new hook attachment */ 516 int (*rcvdata)(hook_p hook, /* Receive data on a hook */ 517 struct mbuf *m, /* The data in an mbuf */ 518 meta_p meta); /* Meta-data, if any */ 519 int (*disconnect)(hook_p hook); /* Notify disconnection of hook */ 520 521 /** How to convert control messages binary <-> ASCII */ 522 const struct ng_cmdlist *cmdlist; /* Optional; may be NULL */ 523}; 524.Ed 525.Pp 526Control messages have the following structure: 527.Bd -literal 528#define NG_CMDSTRSIZ 16 /* Max command string (including null) */ 529 530struct ng_mesg { 531 struct ng_msghdr { 532 u_char version; /* Must equal NG_VERSION */ 533 u_char spare; /* Pad to 2 bytes */ 534 u_short arglen; /* Length of cmd/resp data */ 535 u_long flags; /* Message status flags */ 536 u_long token; /* Reply should have the same token */ 537 u_long typecookie; /* Node type understanding this message */ 538 u_long cmd; /* Command identifier */ 539 u_char cmdstr[NG_CMDSTRSIZ]; /* Cmd string (for debug) */ 540 } header; 541 char data[0]; /* Start of cmd/resp data */ 542}; 543 544#define NG_VERSION 1 /* Netgraph version */ 545#define NGF_ORIG 0x0000 /* Command */ 546#define NGF_RESP 0x0001 /* Response */ 547.Ed 548.Pp 549Control messages have the fixed header shown above, followed by a 550variable length data section which depends on the type cookie 551and the command. Each field is explained below: 552.Bl -tag -width xxx 553.It Dv version 554Indicates the version of netgraph itself. The current version is 555.Dv NG_VERSION . 556.It Dv arglen 557This is the length of any extra arguments, which begin at 558.Dv data . 559.It Dv flags 560Indicates whether this is a command or a response control message. 561.It Dv token 562The 563.Dv token 564is a means by which a sender can match a reply message to the 565corresponding command message; the reply always has the same token. 566.Pp 567.It Dv typecookie 568The corresponding node type's unique 32-bit value. 569If a node doesn't recognize the type cookie it must reject the message 570by returning 571.Er EINVAL . 572.Pp 573Each type should have an include file that defines the commands, 574argument format, and cookie for its own messages. 575The typecookie 576insures that the same header file was included by both sender and 577receiver; when an incompatible change in the header file is made, 578the typecookie 579.Em must 580be changed. 581The de facto method for generating unique type cookies is to take the 582seconds from the epoch at the time the header file is written 583(i.e., the output of 584.Dv "date -u +'%s'" ) . 585.Pp 586There is a predefined typecookie 587.Dv NGM_GENERIC_COOKIE 588for the 589.Dq generic 590node type, and 591a corresponding set of generic messages which all nodes understand. 592The handling of these messages is automatic. 593.It Dv command 594The identifier for the message command. This is type specific, 595and is defined in the same header file as the typecookie. 596.It Dv cmdstr 597Room for a short human readable version of 598.Dq command 599(for debugging purposes only). 600.El 601.Pp 602Some modules may choose to implement messages from more than one 603of the header files and thus recognize more than one type cookie. 604.Sh Control Message ASCII Form 605Control messages are in binary format for efficiency. However, for 606debugging and human interface purposes, and if the node type supports 607it, control messages may be converted to and from an equivalent 608.Tn ASCII 609form. The 610.Tn ASCII 611form is similar to the binary form, with two exceptions: 612.Pp 613.Bl -tag -compact -width xxx 614.It o 615The 616.Dv cmdstr 617header field must contain the 618.Tn ASCII 619name of the command, corresponding to the 620.Dv cmd 621header field. 622.It o 623The 624.Dv args 625field contains a NUL-terminated 626.Tn ASCII 627string version of the message arguments. 628.El 629.Pp 630In general, the arguments field of a control message can be any 631arbitrary C data type. Netgraph includes parsing routines to support 632some pre-defined datatypes in 633.Tn ASCII 634with this simple syntax: 635.Pp 636.Bl -tag -compact -width xxx 637.It o 638Integer types are represented by base 8, 10, or 16 numbers. 639.It o 640Strings are enclosed in double quotes and respect the normal 641C language backslash escapes. 642.It o 643IP addresses have the obvious form. 644.It o 645Arrays are enclosed in square brackets, with the elements listed 646consecutively starting at index zero. An element may have an optional 647index and equals sign preceding it. Whenever an element 648does not have an explicit index, the index is implicitly the previous 649element's index plus one. 650.It o 651Structures are enclosed in curly braces, and each field is specified 652in the form 653.Dq fieldname=value . 654.It o 655Any array element or structure field whose value is equal to its 656.Dq default value 657may be omitted. For integer types, the default value 658is usually zero; for string types, the empty string. 659.It o 660Array elements and structure fields may be specified in any order. 661.El 662.Pp 663Each node type may define its own arbitrary types by providing 664the necessary routines to parse and unparse. 665.Tn ASCII 666forms defined 667for a specific node type are documented in the documentation for 668that node type. 669.Sh Generic Control Messages 670There are a number of standard predefined messages that will work 671for any node, as they are supported directly by the framework itself. 672These are defined in 673.In netgraph/ng_message.h 674along with the basic layout of messages and other similar information. 675.Bl -tag -width xxx 676.It Dv NGM_CONNECT 677Connect to another node, using the supplied hook names on either end. 678.It Dv NGM_MKPEER 679Construct a node of the given type and then connect to it using the 680supplied hook names. 681.It Dv NGM_SHUTDOWN 682The target node should disconnect from all its neighbours and shut down. 683Persistent nodes such as those representing physical hardware 684might not disappear from the node namespace, but only reset themselves. 685The node must disconnect all of its hooks. 686This may result in neighbors shutting themselves down, and possibly a 687cascading shutdown of the entire connected graph. 688.It Dv NGM_NAME 689Assign a name to a node. Nodes can exist without having a name, and this 690is the default for nodes created using the 691.Dv NGM_MKPEER 692method. Such nodes can only be addressed relatively or by their ID number. 693.It Dv NGM_RMHOOK 694Ask the node to break a hook connection to one of its neighbours. 695Both nodes will have their 696.Dq disconnect 697method invoked. 698Either node may elect to totally shut down as a result. 699.It Dv NGM_NODEINFO 700Asks the target node to describe itself. The four returned fields 701are the node name (if named), the node type, the node ID and the 702number of hooks attached. The ID is an internal number unique to that node. 703.It Dv NGM_LISTHOOKS 704This returns the information given by 705.Dv NGM_NODEINFO , 706but in addition 707includes an array of fields describing each link, and the description for 708the node at the far end of that link. 709.It Dv NGM_LISTNAMES 710This returns an array of node descriptions (as for 711.Dv NGM_NODEINFO ")" 712where each entry of the array describes a named node. 713All named nodes will be described. 714.It Dv NGM_LISTNODES 715This is the same as 716.Dv NGM_LISTNAMES 717except that all nodes are listed regardless of whether they have a name or not. 718.It Dv NGM_LISTTYPES 719This returns a list of all currently installed netgraph types. 720.It Dv NGM_TEXT_STATUS 721The node may return a text formatted status message. 722The status information is determined entirely by the node type. 723It is the only "generic" message 724that requires any support within the node itself and as such the node may 725elect to not support this message. The text response must be less than 726.Dv NG_TEXTRESPONSE 727bytes in length (presently 1024). This can be used to return general 728status information in human readable form. 729.It Dv NGM_BINARY2ASCII 730This message converts a binary control message to its 731.Tn ASCII 732form. 733The entire control message to be converted is contained within the 734arguments field of the 735.Dv NGM_BINARY2ASCII 736message itself. If successful, the reply will contain the same control 737message in 738.Tn ASCII 739form. 740A node will typically only know how to translate messages that it 741itself understands, so the target node of the 742.Dv NGM_BINARY2ASCII 743is often the same node that would actually receive that message. 744.It Dv NGM_ASCII2BINARY 745The opposite of 746.Dv NGM_BINARY2ASCII . 747The entire control message to be converted, in 748.Tn ASCII 749form, is contained 750in the arguments section of the 751.Dv NGM_ASCII2BINARY 752and need only have the 753.Dv flags , 754.Dv cmdstr , 755and 756.Dv arglen 757header fields filled in, plus the NUL-terminated string version of 758the arguments in the arguments field. If successful, the reply 759contains the binary version of the control message. 760.El 761.Sh Metadata 762Data moving through the 763.Nm 764system can be accompanied by meta-data that describes some 765aspect of that data. The form of the meta-data is a fixed header, 766which contains enough information for most uses, and can optionally 767be supplemented by trailing 768.Em option 769structures, which contain a 770.Em cookie 771(see the section on control messages), an identifier, a length and optional 772data. If a node does not recognize the cookie associated with an option, 773it should ignore that option. 774.Pp 775Meta data might include such things as priority, discard eligibility, 776or special processing requirements. It might also mark a packet for 777debug status, etc. The use of meta-data is still experimental. 778.Sh INITIALIZATION 779The base 780.Nm 781code may either be statically compiled 782into the kernel or else loaded dynamically as a KLD via 783.Xr kldload 8 . 784In the former case, include 785.Pp 786.D1 Cd options NETGRAPH 787.Pp 788in your kernel configuration file. You may also include selected 789node types in the kernel compilation, for example: 790.Bd -unfilled -offset indent 791.Cd options NETGRAPH 792.Cd options NETGRAPH_SOCKET 793.Cd options NETGRAPH_ECHO 794.Ed 795.Pp 796Once the 797.Nm 798subsystem is loaded, individual node types may be loaded at any time 799as KLD modules via 800.Xr kldload 8 . 801Moreover, 802.Nm 803knows how to automatically do this; when a request to create a new 804node of unknown type 805.Em type 806is made, 807.Nm 808will attempt to load the KLD module 809.Pa ng_type.ko . 810.Pp 811Types can also be installed at boot time, as certain device drivers 812may want to export each instance of the device as a netgraph node. 813.Pp 814In general, new types can be installed at any time from within the 815kernel by calling 816.Fn ng_newtype , 817supplying a pointer to the type's 818.Dv struct ng_type 819structure. 820.Pp 821The 822.Fn NETGRAPH_INIT 823macro automates this process by using a linker set. 824.Sh EXISTING NODE TYPES 825Several node types currently exist. Each is fully documented 826in its own man page: 827.Bl -tag -width xxx 828.It SOCKET 829The socket type implements two new sockets in the new protocol domain 830.Dv PF_NETGRAPH . 831The new sockets protocols are 832.Dv NG_DATA 833and 834.Dv NG_CONTROL , 835both of type 836.Dv SOCK_DGRAM . 837Typically one of each is associated with a socket node. 838When both sockets have closed, the node will shut down. The 839.Dv NG_DATA 840socket is used for sending and receiving data, while the 841.Dv NG_CONTROL 842socket is used for sending and receiving control messages. 843Data and control messages are passed using the 844.Xr sendto 2 845and 846.Xr recvfrom 2 847calls, using a 848.Dv struct sockaddr_ng 849socket address. 850.Pp 851.It HOLE 852Responds only to generic messages and is a 853.Dq black hole 854for data, Useful for testing. Always accepts new hooks. 855.Pp 856.It ECHO 857Responds only to generic messages and always echoes data back through the 858hook from which it arrived. Returns any non generic messages as their 859own response. Useful for testing. Always accepts new hooks. 860.Pp 861.It TEE 862This node is useful for 863.Dq snooping . 864It has 4 hooks: 865.Dv left , 866.Dv right , 867.Dv left2right , 868and 869.Dv right2left . 870Data entering from the right is passed to the left and duplicated on 871.Dv right2left , 872and data entering from the left is passed to the right and 873duplicated on 874.Dv left2right . 875Data entering from 876.Dv left2right 877is sent to the right and data from 878.Dv right2left 879to left. 880.Pp 881.It RFC1490 MUX 882Encapsulates/de-encapsulates frames encoded according to RFC 1490. 883Has a hook for the encapsulated packets 884.Pq Dq downstream 885and one hook 886for each protocol (i.e., IP, PPP, etc.). 887.Pp 888.It FRAME RELAY MUX 889Encapsulates/de-encapsulates Frame Relay frames. 890Has a hook for the encapsulated packets 891.Pq Dq downstream 892and one hook 893for each DLCI. 894.Pp 895.It FRAME RELAY LMI 896Automatically handles frame relay 897.Dq LMI 898(link management interface) operations and packets. 899Automatically probes and detects which of several LMI standards 900is in use at the exchange. 901.Pp 902.It TTY 903This node is also a line discipline. It simply converts between mbuf 904frames and sequential serial data, allowing a tty to appear as a netgraph 905node. It has a programmable 906.Dq hotkey 907character. 908.Pp 909.It ASYNC 910This node encapsulates and de-encapsulates asynchronous frames 911according to RFC 1662. This is used in conjunction with the TTY node 912type for supporting PPP links over asynchronous serial lines. 913.Pp 914.It INTERFACE 915This node is also a system networking interface. It has hooks representing 916each protocol family (IP, AppleTalk, IPX, etc.) and appears in the output of 917.Xr ifconfig 8 . 918The interfaces are named 919.Em ng0 , 920.Em ng1 , 921etc. 922.El 923.Sh NOTES 924Whether a named node exists can be checked by trying to send a control message 925to it (e.g., 926.Dv NGM_NODEINFO ) . 927If it does not exist, 928.Er ENOENT 929will be returned. 930.Pp 931All data messages are mbuf chains with the M_PKTHDR flag set. 932.Pp 933Nodes are responsible for freeing what they allocate. 934There are three exceptions: 935.Bl -tag -width xxxx 936.It 1 937Mbufs sent across a data link are never to be freed by the sender. 938.It 2 939Any meta-data information traveling with the data has the same restriction. 940It might be freed by any node the data passes through, and a 941.Dv NULL 942passed onwards, but the caller will never free it. 943Two macros 944.Fn NG_FREE_META "meta" 945and 946.Fn NG_FREE_DATA "m" "meta" 947should be used if possible to free data and meta data (see 948.In netgraph/netgraph.h ) . 949.It 3 950Messages sent using 951.Fn ng_send_msg 952are freed by the callee. As in the case above, the addresses 953associated with the message are freed by whatever allocated them so the 954recipient should copy them if it wants to keep that information. 955.El 956.Sh FILES 957.Bl -tag -width xxxxx -compact 958.It In netgraph/netgraph.h 959Definitions for use solely within the kernel by 960.Nm 961nodes. 962.It In netgraph/ng_message.h 963Definitions needed by any file that needs to deal with 964.Nm 965messages. 966.It In netgraph/socket/ng_socket.h 967Definitions needed to use 968.Nm 969socket type nodes. 970.It In netgraph/{type}/ng_{type}.h 971Definitions needed to use 972.Nm 973{type} 974nodes, including the type cookie definition. 975.It Pa /boot/modules/netgraph.ko 976Netgraph subsystem loadable KLD module. 977.It Pa /boot/modules/ng_{type}.ko 978Loadable KLD module for node type {type}. 979.El 980.Sh USER MODE SUPPORT 981There is a library for supporting user-mode programs that wish 982to interact with the netgraph system. See 983.Xr netgraph 3 984for details. 985.Pp 986Two user-mode support programs, 987.Xr ngctl 8 988and 989.Xr nghook 8 , 990are available to assist manual configuration and debugging. 991.Pp 992There are a few useful techniques for debugging new node types. 993First, implementing new node types in user-mode first 994makes debugging easier. 995The 996.Em tee 997node type is also useful for debugging, especially in conjunction with 998.Xr ngctl 8 999and 1000.Xr nghook 8 . 1001.Sh SEE ALSO 1002.Xr socket 2 , 1003.Xr netgraph 3 , 1004.Xr ng_async 4 , 1005.Xr ng_bpf 4 , 1006.Xr ng_bridge 4 , 1007.Xr ng_cisco 4 , 1008.Xr ng_echo 4 , 1009.Xr ng_eiface 4 , 1010.Xr ng_etf 4 , 1011.Xr ng_ether 4 , 1012.Xr ng_frame_relay 4 , 1013.Xr ng_hole 4 , 1014.Xr ng_iface 4 , 1015.Xr ng_ksocket 4 , 1016.Xr ng_l2tp 4 , 1017.Xr ng_lmi 4 , 1018.Xr ng_mppc 4 , 1019.Xr ng_one2many 4 , 1020.Xr ng_ppp 4 , 1021.Xr ng_pppoe 4 , 1022.Xr ng_rfc1490 4 , 1023.Xr ng_socket 4 , 1024.Xr ng_tee 4 , 1025.Xr ng_tty 4 , 1026.Xr ng_UI 4 , 1027.Xr ng_vjc 4 , 1028.Xr ngctl 8 , 1029.Xr nghook 8 1030.Sh HISTORY 1031The 1032.Nm 1033system was designed and first implemented at Whistle Communications, Inc.\& 1034in a version of 1035.Fx 2.2 1036customized for the Whistle InterJet. 1037It first made its debut in the main tree in 1038.Fx 3.4 . 1039.Sh AUTHORS 1040.An -nosplit 1041.An Julian Elischer Aq Mt julian@FreeBSD.org , 1042with contributions by 1043.An Archie Cobbs Aq Mt archie@FreeBSD.org . 1044