README 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # This file contains some examples of using NCD, the Network Configuration Daemon.
  2. #
  3. # A short introduction to NCD follows.
  4. #
  5. # NCD is a general-purpose system configuration system, operated with a unique programming language.
  6. # The configuration consists of one or more so-called processes that can be considered executing in
  7. # parallel. Further, each process consists of one or more statements, representing the individual
  8. # actions. Statements are implemented as modules built into NCD.
  9. #
  10. # Inside a process, statements can be considered "executed" one after another. That is, when NCD
  11. # starts up, it initializes the first statement, putting it in the DOWN state. When the statement
  12. # reports having transitioned into the UP state, it initializes the next statement in the DOWN state,
  13. # and so on.
  14. #
  15. # However, execution can go in the other direction too. A statement in the UP state can, at any time,
  16. # report having transitioned into the DOWN state. At this point, any statements after that one will
  17. # automatically be de-initialized. The de-initiazation is done from the bottom up. First the last
  18. # initialized statement after the problematic statement is requested to terminate and enters the
  19. # DYING state. After it terminates, its preceding statement enters the DYING state, and so on, until
  20. # all statements following the problematic statement have been de-initiazed.
  21. #
  22. # The backward-execution is the key feature of NCD, and is particularly well suited for programming
  23. # system configurations. Read on to see why.
  24. #
  25. # Statements in NCD can be divided into two categories:
  26. # - Statements that configure something. These statements transition into the UP state "immediately".
  27. # On de-initialization, such statements perform the reverse operation of what they did when initialized.
  28. # Imaginary example: a statement that turn a light on intialization, and turns if off on de-initialization.
  29. # - Statements that wait for something. These statements may remain in the DOWN state indefinitely.
  30. # They enter the UP state when the waited-for condition is satisfied, and also go back into the DOWN
  31. # state when it is no longer satisfied.
  32. # Imaginary example: a statement that is UP when a switch is turned on, and DOWN when it is turned off.
  33. #
  34. # Using the two example statements, we can constuct a process that controls the light based on the switch:
  35. # (these are not really implemented in NCD :)
  36. #
  37. # process light {
  38. # wait_switch();
  39. # turn_light();
  40. # }
  41. #
  42. # When the switch is turned on, wait_switch() will transition to UP, initializing turn_light(), turning the
  43. # light on. When the switch is turned off, wait_switch() will transition to DOWN, causing the de-initialization
  44. # of turn_light(), turning the light off.
  45. # We can add another turn_light() at the end to make the switch control two lights.
  46. #
  47. # A more complex example: We have a christmas three with lights on it. There are multiple "regular" lights,
  48. # controlled with switches, and a special "top" light. The regular lights take a long time to turn on, and
  49. # each takes a different, unpredictable time. We want the top light to be turned on if and only if all the regular
  50. # lights are completely on.
  51. #
  52. # This problem can easily be solved using dependencies. NCD has built-in support for dependencies, provided
  53. # in the form of provide() and depend() statements. A depend() statement is DOWN when its corresponding
  54. # provide() statement is not initialized, and UP when it is. When a provide() is requested to de-initialize, it
  55. # transitions the depend() statements back into the DOWN state, and, before actually dying, waits for any
  56. # statements following them to de-initialize.
  57. #
  58. # The christmas three problem can then be programmed as follows:
  59. #
  60. # process light1 {
  61. # wait_switch1();
  62. # turn_light1();
  63. # provide("L1");
  64. # }
  65. #
  66. # process light2 {
  67. # wait_switch2();
  68. # turn_light2();
  69. # provide("L2");
  70. # }
  71. #
  72. # process top_light {
  73. # depend("L1");
  74. # depend("L2");
  75. # turn_top_light();
  76. # }
  77. #
  78. # Follow some real examples of network configuration using NCD.
  79. # For a list of implemented statements and their descriptions, take a look at the BadVPN source code, in
  80. # the ncd/modules/ folder.
  81. #
  82. # Network card using DHCP
  83. process lan {
  84. # Make the interface name a variable so we can refer to it.
  85. # The NCD language has no notion of assigning a variable. Instead variables are
  86. # provided by statements preceding the statement where they are used.
  87. # The built-in var() statement can be used to make an alias.
  88. var("eth0") dev;
  89. # Wait for the network card appear and for the cable to be plugged in.
  90. net.backend.physical(dev);
  91. # Start DHCP.
  92. net.ipv4.dhcp(dev) dhcp;
  93. # Once DHCP obtains an IP address, assign it to the interface.
  94. net.ipv4.addr(dev, dhcp.addr, dhcp.prefix);
  95. # Add a default route.
  96. # <dest> <dest_prefix> <gateway/"none"> <metric> <device>
  97. net.ipv4.route("0.0.0.0", "0", dhcp.gateway, "20", dev);
  98. # Add DNS servers, as provided by DHCP.
  99. # "20" is the priority of the servers. When applying DNS servers, NCD collects the servers
  100. # from all active net.dns() statements, sorts them by priority ascending (stable), and writes
  101. # them to /etc/resolv.conf, overwriting anything that was previously there.
  102. net.dns(dhcp.dns_servers, "20");
  103. }
  104. # Network card with static configuration
  105. process lan2 {
  106. # Make the interface name a variable so we can refer to it.
  107. var("eth1") dev;
  108. # Wait for the network card appear and for the cable to be plugged in.
  109. net.backend.physical(dev);
  110. # Assign an IP address.
  111. # "24" is prefix length, i.e. subnet mask 255.255.255.0
  112. net.ipv4.addr(dev, "192.168.62.3", "24");
  113. # Add a default route.
  114. net.ipv4.route("0.0.0.0", "0", "192.168.62.3", "20", dev);
  115. # Build a list of DNS servers.
  116. # The NCD language does not support "expressions" - statement arguments must be
  117. # constants or variables referring to preceding statements.
  118. # A list can be constructed using the built-in list() statement.
  119. list("192.168.62.5", "192.168.62.6") dns_servers;
  120. # Add the DNS servers.
  121. net.dns(dns_servers, "20");
  122. }
  123. #
  124. # A BadVPN VPN interface for access to the virtual
  125. # network (only).
  126. #
  127. process lan {
  128. ... (something like above) ...
  129. # Alias our IP address for easy access from the "vpn" process (or, for a static address, alias
  130. # it before assigning it, and assign it using the alias).
  131. var(dhcp.addr) ipaddr;
  132. # Allow VPN to start at this point.
  133. # (and require it to stop before deconfiguring the interface if e.g. the cable is plugged out)
  134. provide("LAN");
  135. }
  136. process vpn {
  137. # Need the local interface to be working in order start VPN.
  138. depend("LAN") landep;
  139. # Choose the name of the network interface.
  140. var("tap3") dev;
  141. # Construct command line arguments for badvpn-client. Adapt according to your setup.
  142. # "--tapdev" will be provided automatically.
  143. # Alias the port number that the VPN process will bind to.
  144. var("6000") port;
  145. # Construct dynamic parts of command line options.
  146. # The VPN client program needs to know some IP addresses in order to tell other peers where to connect to.
  147. # Obtain this informations from variables in the "lan" process through the depend() statement. TODO: not implemented yet!
  148. # Construct the local address (addr + port).
  149. concat(landep.ipaddr, ":", port) local_addr_arg;
  150. # Construct the Internet address (assuming we are behind a NAT).
  151. # Need to know the NAT's external address here. But we could implement a statement that queried it somehow.
  152. # That is if we have preconfigured the NAT router to forward ports. But we could implement a statement
  153. # that obtains the mappings dynamically with UPnP!
  154. concat("1.2.3.4", ":", port) internet_addr_arg;
  155. # Finally construct the complete arguments, using the above address arguments.
  156. list(
  157. "--logger", "syslog", "--syslog-ident", "badvpn",
  158. "--server-addr", "badvpn.example.com:7000",
  159. "--ssl", "--nssdb", "sql:/home/badvpn/nssdb", "--client-cert-name", "peer-someone",
  160. "--transport-mode", "udp", "--encryption-mode", "blowfish", "--hash-mode", "md5", "--otp", "blowfish", "3000", "2000",
  161. "--scope", "mylan", "--scope", "internet",
  162. "--bind-addr", "0.0.0.0:6000", "--num-ports", "20",
  163. "--ext-addr", local_addr_arg, "mylan",
  164. "--ext-addr", internet_addr_arg, "internet"
  165. ) args;
  166. # Start the BadVPN backend.
  167. net.backend.badvpn(dev, "badvpn", "/usr/bin/badvpn-client-26", args);
  168. # Assign an IP address to the VPN interface.
  169. # (we could easily use DHCP here!)
  170. net.ipv4.addr(dev, "10.0.0.1", "24");
  171. }