if.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Interface related header.
  2. Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #ifndef _ZEBRA_IF_H
  17. #define _ZEBRA_IF_H
  18. #include "zebra.h"
  19. #include "linklist.h"
  20. /* Interface link-layer type, if known. Derived from:
  21. *
  22. * net/if_arp.h on various platforms - Linux especially.
  23. * http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml
  24. *
  25. * Some of the more obviously defunct technologies left out.
  26. */
  27. enum zebra_link_type {
  28. ZEBRA_LLT_UNKNOWN = 0,
  29. ZEBRA_LLT_ETHER,
  30. ZEBRA_LLT_EETHER,
  31. ZEBRA_LLT_AX25,
  32. ZEBRA_LLT_PRONET,
  33. ZEBRA_LLT_IEEE802,
  34. ZEBRA_LLT_ARCNET,
  35. ZEBRA_LLT_APPLETLK,
  36. ZEBRA_LLT_DLCI,
  37. ZEBRA_LLT_ATM,
  38. ZEBRA_LLT_METRICOM,
  39. ZEBRA_LLT_IEEE1394,
  40. ZEBRA_LLT_EUI64,
  41. ZEBRA_LLT_INFINIBAND,
  42. ZEBRA_LLT_SLIP,
  43. ZEBRA_LLT_CSLIP,
  44. ZEBRA_LLT_SLIP6,
  45. ZEBRA_LLT_CSLIP6,
  46. ZEBRA_LLT_RSRVD,
  47. ZEBRA_LLT_ADAPT,
  48. ZEBRA_LLT_ROSE,
  49. ZEBRA_LLT_X25,
  50. ZEBRA_LLT_PPP,
  51. ZEBRA_LLT_CHDLC,
  52. ZEBRA_LLT_LAPB,
  53. ZEBRA_LLT_RAWHDLC,
  54. ZEBRA_LLT_IPIP,
  55. ZEBRA_LLT_IPIP6,
  56. ZEBRA_LLT_FRAD,
  57. ZEBRA_LLT_SKIP,
  58. ZEBRA_LLT_LOOPBACK,
  59. ZEBRA_LLT_LOCALTLK,
  60. ZEBRA_LLT_FDDI,
  61. ZEBRA_LLT_SIT,
  62. ZEBRA_LLT_IPDDP,
  63. ZEBRA_LLT_IPGRE,
  64. ZEBRA_LLT_IP6GRE,
  65. ZEBRA_LLT_PIMREG,
  66. ZEBRA_LLT_HIPPI,
  67. ZEBRA_LLT_ECONET,
  68. ZEBRA_LLT_IRDA,
  69. ZEBRA_LLT_FCPP,
  70. ZEBRA_LLT_FCAL,
  71. ZEBRA_LLT_FCPL,
  72. ZEBRA_LLT_FCFABRIC,
  73. ZEBRA_LLT_IEEE802_TR,
  74. ZEBRA_LLT_IEEE80211,
  75. ZEBRA_LLT_IEEE80211_RADIOTAP,
  76. ZEBRA_LLT_IEEE802154,
  77. ZEBRA_LLT_IEEE802154_PHY,
  78. };
  79. /*
  80. Interface name length.
  81. Linux define value in /usr/include/linux/if.h.
  82. #define IFNAMSIZ 16
  83. FreeBSD define value in /usr/include/net/if.h.
  84. #define IFNAMSIZ 16
  85. */
  86. #define INTERFACE_NAMSIZ 20
  87. #define INTERFACE_HWADDR_MAX 20
  88. typedef signed int ifindex_t;
  89. #ifdef HAVE_PROC_NET_DEV
  90. struct if_stats
  91. {
  92. unsigned long rx_packets; /* total packets received */
  93. unsigned long tx_packets; /* total packets transmitted */
  94. unsigned long rx_bytes; /* total bytes received */
  95. unsigned long tx_bytes; /* total bytes transmitted */
  96. unsigned long rx_errors; /* bad packets received */
  97. unsigned long tx_errors; /* packet transmit problems */
  98. unsigned long rx_dropped; /* no space in linux buffers */
  99. unsigned long tx_dropped; /* no space available in linux */
  100. unsigned long rx_multicast; /* multicast packets received */
  101. unsigned long rx_compressed;
  102. unsigned long tx_compressed;
  103. unsigned long collisions;
  104. /* detailed rx_errors: */
  105. unsigned long rx_length_errors;
  106. unsigned long rx_over_errors; /* receiver ring buff overflow */
  107. unsigned long rx_crc_errors; /* recved pkt with crc error */
  108. unsigned long rx_frame_errors; /* recv'd frame alignment error */
  109. unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  110. unsigned long rx_missed_errors; /* receiver missed packet */
  111. /* detailed tx_errors */
  112. unsigned long tx_aborted_errors;
  113. unsigned long tx_carrier_errors;
  114. unsigned long tx_fifo_errors;
  115. unsigned long tx_heartbeat_errors;
  116. unsigned long tx_window_errors;
  117. };
  118. #endif /* HAVE_PROC_NET_DEV */
  119. /* Here are "non-official" architectural constants. */
  120. #define TE_EXT_MASK 0x0FFFFFFF
  121. #define TE_EXT_ANORMAL 0x80000000
  122. #define LOSS_PRECISION 0.000003
  123. #define TE_KILO_BIT 1000
  124. #define TE_BYTE 8
  125. #define DEFAULT_BANDWIDTH 10000
  126. #define MAX_CLASS_TYPE 8
  127. #define MAX_PKT_LOSS 50.331642
  128. /* Link Parameters Status: 0: unset, 1: set, */
  129. #define LP_UNSET 0x0000
  130. #define LP_TE 0x0001
  131. #define LP_MAX_BW 0x0002
  132. #define LP_MAX_RSV_BW 0x0004
  133. #define LP_UNRSV_BW 0x0008
  134. #define LP_ADM_GRP 0x0010
  135. #define LP_RMT_AS 0x0020
  136. #define LP_DELAY 0x0040
  137. #define LP_MM_DELAY 0x0080
  138. #define LP_DELAY_VAR 0x0100
  139. #define LP_PKT_LOSS 0x0200
  140. #define LP_RES_BW 0x0400
  141. #define LP_AVA_BW 0x0800
  142. #define LP_USE_BW 0x1000
  143. #define IS_PARAM_UNSET(lp, st) !(lp->lp_status & st)
  144. #define IS_PARAM_SET(lp, st) (lp->lp_status & st)
  145. #define IS_LINK_PARAMS_SET(lp) (lp->lp_status != LP_UNSET)
  146. #define SET_PARAM(lp, st) (lp->lp_status) |= (st)
  147. #define UNSET_PARAM(lp, st) (lp->lp_status) &= ~(st)
  148. #define RESET_LINK_PARAM(lp) (lp->lp_status = LP_UNSET)
  149. /* Link Parameters for Traffic Engineering */
  150. struct if_link_params {
  151. u_int32_t lp_status; /* Status of Link Parameters: */
  152. u_int32_t te_metric; /* Traffic Engineering metric */
  153. float max_bw; /* Maximum Bandwidth */
  154. float max_rsv_bw; /* Maximum Reservable Bandwidth */
  155. float unrsv_bw[MAX_CLASS_TYPE]; /* Unreserved Bandwidth per Class Type (8) */
  156. u_int32_t admin_grp; /* Administrative group */
  157. u_int32_t rmt_as; /* Remote AS number */
  158. struct in_addr rmt_ip; /* Remote IP address */
  159. u_int32_t av_delay; /* Link Average Delay */
  160. u_int32_t min_delay; /* Link Min Delay */
  161. u_int32_t max_delay; /* Link Max Delay */
  162. u_int32_t delay_var; /* Link Delay Variation */
  163. float pkt_loss; /* Link Packet Loss */
  164. float res_bw; /* Residual Bandwidth */
  165. float ava_bw; /* Available Bandwidth */
  166. float use_bw; /* Utilized Bandwidth */
  167. };
  168. #define INTERFACE_LINK_PARAMS_SIZE sizeof(struct if_link_params)
  169. #define HAS_LINK_PARAMS(ifp) ((ifp)->link_params != NULL)
  170. /* Interface structure */
  171. struct interface
  172. {
  173. /* Interface name. This should probably never be changed after the
  174. interface is created, because the configuration info for this interface
  175. is associated with this structure. For that reason, the interface
  176. should also never be deleted (to avoid losing configuration info).
  177. To delete, just set ifindex to IFINDEX_INTERNAL to indicate that the
  178. interface does not exist in the kernel.
  179. */
  180. char name[INTERFACE_NAMSIZ + 1];
  181. /* Interface index (should be IFINDEX_INTERNAL for non-kernel or
  182. deleted interfaces). */
  183. ifindex_t ifindex;
  184. #define IFINDEX_INTERNAL 0
  185. /* Zebra internal interface status */
  186. u_char status;
  187. #define ZEBRA_INTERFACE_ACTIVE (1 << 0)
  188. #define ZEBRA_INTERFACE_SUB (1 << 1)
  189. #define ZEBRA_INTERFACE_LINKDETECTION (1 << 2)
  190. /* Interface flags. */
  191. uint64_t flags;
  192. /* Interface metric */
  193. int metric;
  194. /* Interface MTU. */
  195. unsigned int mtu; /* IPv4 MTU */
  196. unsigned int mtu6; /* IPv6 MTU - probably, but not neccessarily same as mtu */
  197. /* Link-layer information and hardware address */
  198. enum zebra_link_type ll_type;
  199. u_char hw_addr[INTERFACE_HWADDR_MAX];
  200. int hw_addr_len;
  201. /* interface bandwidth, kbits */
  202. unsigned int bandwidth;
  203. /* Link parameters for Traffic Engineering */
  204. struct if_link_params *link_params;
  205. /* description of the interface. */
  206. char *desc;
  207. /* Distribute list. */
  208. void *distribute_in;
  209. void *distribute_out;
  210. /* Connected address list. */
  211. struct list *connected;
  212. /* Daemon specific interface data pointer. */
  213. void *info;
  214. /* Statistics fileds. */
  215. #ifdef HAVE_PROC_NET_DEV
  216. struct if_stats stats;
  217. #endif /* HAVE_PROC_NET_DEV */
  218. #ifdef HAVE_NET_RT_IFLIST
  219. struct if_data stats;
  220. #endif /* HAVE_NET_RT_IFLIST */
  221. vrf_id_t vrf_id;
  222. };
  223. /* Connected address structure. */
  224. struct connected
  225. {
  226. /* Attached interface. */
  227. struct interface *ifp;
  228. /* Flags for configuration. */
  229. u_char conf;
  230. #define ZEBRA_IFC_REAL (1 << 0)
  231. #define ZEBRA_IFC_CONFIGURED (1 << 1)
  232. #define ZEBRA_IFC_QUEUED (1 << 2)
  233. /*
  234. The ZEBRA_IFC_REAL flag should be set if and only if this address
  235. exists in the kernel and is actually usable. (A case where it exists but
  236. is not yet usable would be IPv6 with DAD)
  237. The ZEBRA_IFC_CONFIGURED flag should be set if and only if this address
  238. was configured by the user from inside quagga.
  239. The ZEBRA_IFC_QUEUED flag should be set if and only if the address exists
  240. in the kernel. It may and should be set although the address might not be
  241. usable yet. (compare with ZEBRA_IFC_REAL)
  242. */
  243. /* Flags for connected address. */
  244. u_char flags;
  245. #define ZEBRA_IFA_SECONDARY (1 << 0)
  246. #define ZEBRA_IFA_PEER (1 << 1)
  247. #define ZEBRA_IFA_UNNUMBERED (1 << 2)
  248. /* N.B. the ZEBRA_IFA_PEER flag should be set if and only if
  249. a peer address has been configured. If this flag is set,
  250. the destination field must contain the peer address.
  251. Otherwise, if this flag is not set, the destination address
  252. will either contain a broadcast address or be NULL.
  253. */
  254. /* Address of connected network. */
  255. struct prefix *address;
  256. /* Peer or Broadcast address, depending on whether ZEBRA_IFA_PEER is set.
  257. Note: destination may be NULL if ZEBRA_IFA_PEER is not set. */
  258. struct prefix *destination;
  259. /* Label for Linux 2.2.X and upper. */
  260. char *label;
  261. };
  262. /* Does the destination field contain a peer address? */
  263. #define CONNECTED_PEER(C) CHECK_FLAG((C)->flags, ZEBRA_IFA_PEER)
  264. /* Prefix to insert into the RIB */
  265. #define CONNECTED_PREFIX(C) \
  266. (CONNECTED_PEER(C) ? (C)->destination : (C)->address)
  267. /* Identifying address. We guess that if there's a peer address, but the
  268. local address is in the same prefix, then the local address may be unique. */
  269. #define CONNECTED_ID(C) \
  270. ((CONNECTED_PEER(C) && !prefix_match((C)->destination, (C)->address)) ?\
  271. (C)->destination : (C)->address)
  272. /* Interface hook sort. */
  273. #define IF_NEW_HOOK 0
  274. #define IF_DELETE_HOOK 1
  275. /* There are some interface flags which are only supported by some
  276. operating system. */
  277. #ifndef IFF_NOTRAILERS
  278. #define IFF_NOTRAILERS 0x0
  279. #endif /* IFF_NOTRAILERS */
  280. #ifndef IFF_OACTIVE
  281. #define IFF_OACTIVE 0x0
  282. #endif /* IFF_OACTIVE */
  283. #ifndef IFF_SIMPLEX
  284. #define IFF_SIMPLEX 0x0
  285. #endif /* IFF_SIMPLEX */
  286. #ifndef IFF_LINK0
  287. #define IFF_LINK0 0x0
  288. #endif /* IFF_LINK0 */
  289. #ifndef IFF_LINK1
  290. #define IFF_LINK1 0x0
  291. #endif /* IFF_LINK1 */
  292. #ifndef IFF_LINK2
  293. #define IFF_LINK2 0x0
  294. #endif /* IFF_LINK2 */
  295. #ifndef IFF_NOXMIT
  296. #define IFF_NOXMIT 0x0
  297. #endif /* IFF_NOXMIT */
  298. #ifndef IFF_NORTEXCH
  299. #define IFF_NORTEXCH 0x0
  300. #endif /* IFF_NORTEXCH */
  301. #ifndef IFF_IPV4
  302. #define IFF_IPV4 0x0
  303. #endif /* IFF_IPV4 */
  304. #ifndef IFF_IPV6
  305. #define IFF_IPV6 0x0
  306. #endif /* IFF_IPV6 */
  307. #ifndef IFF_VIRTUAL
  308. #define IFF_VIRTUAL 0x0
  309. #endif /* IFF_VIRTUAL */
  310. /* Prototypes. */
  311. extern int if_cmp_func (struct interface *, struct interface *);
  312. extern struct interface *if_create (const char *name, int namelen);
  313. extern struct interface *if_lookup_by_index (ifindex_t);
  314. extern struct interface *if_lookup_exact_address (struct in_addr);
  315. extern struct interface *if_lookup_address (struct in_addr);
  316. extern struct interface *if_lookup_prefix (struct prefix *prefix);
  317. extern struct interface *if_create_vrf (const char *name, int namelen,
  318. vrf_id_t vrf_id);
  319. extern struct interface *if_lookup_by_index_vrf (ifindex_t, vrf_id_t vrf_id);
  320. extern struct interface *if_lookup_exact_address_vrf (struct in_addr,
  321. vrf_id_t vrf_id);
  322. extern struct interface *if_lookup_address_vrf (struct in_addr,
  323. vrf_id_t vrf_id);
  324. extern struct interface *if_lookup_prefix_vrf (struct prefix *prefix,
  325. vrf_id_t vrf_id);
  326. /* These 2 functions are to be used when the ifname argument is terminated
  327. by a '\0' character: */
  328. extern struct interface *if_lookup_by_name (const char *ifname);
  329. extern struct interface *if_get_by_name (const char *ifname);
  330. extern struct interface *if_lookup_by_name_vrf (const char *ifname,
  331. vrf_id_t vrf_id);
  332. extern struct interface *if_get_by_name_vrf (const char *ifname,
  333. vrf_id_t vrf_id);
  334. /* For these 2 functions, the namelen argument should be the precise length
  335. of the ifname string (not counting any optional trailing '\0' character).
  336. In most cases, strnlen should be used to calculate the namelen value. */
  337. extern struct interface *if_lookup_by_name_len(const char *ifname,
  338. size_t namelen);
  339. extern struct interface *if_get_by_name_len(const char *ifname,size_t namelen);
  340. extern struct interface *if_lookup_by_name_len_vrf(const char *ifname,
  341. size_t namelen, vrf_id_t vrf_id);
  342. extern struct interface *if_get_by_name_len_vrf(const char *ifname,
  343. size_t namelen, vrf_id_t vrf_id);
  344. /* Delete the interface, but do not free the structure, and leave it in the
  345. interface list. It is often advisable to leave the pseudo interface
  346. structure because there may be configuration information attached. */
  347. extern void if_delete_retain (struct interface *);
  348. /* Delete and free the interface structure: calls if_delete_retain and then
  349. deletes it from the interface list and frees the structure. */
  350. extern void if_delete (struct interface *);
  351. extern int if_is_up (struct interface *);
  352. extern int if_is_running (struct interface *);
  353. extern int if_is_operative (struct interface *);
  354. extern int if_is_loopback (struct interface *);
  355. extern int if_is_broadcast (struct interface *);
  356. extern int if_is_pointopoint (struct interface *);
  357. extern int if_is_multicast (struct interface *);
  358. extern void if_add_hook (int, int (*)(struct interface *));
  359. extern void if_init (vrf_id_t, struct list **);
  360. extern void if_terminate (vrf_id_t, struct list **);
  361. extern void if_dump_all (void);
  362. extern const char *if_flag_dump(unsigned long);
  363. extern const char *if_link_type_str (enum zebra_link_type);
  364. /* Please use ifindex2ifname instead of if_indextoname where possible;
  365. ifindex2ifname uses internal interface info, whereas if_indextoname must
  366. make a system call. */
  367. extern const char *ifindex2ifname (ifindex_t);
  368. extern const char *ifindex2ifname_vrf (ifindex_t, vrf_id_t vrf_id);
  369. /* Please use ifname2ifindex instead of if_nametoindex where possible;
  370. ifname2ifindex uses internal interface info, whereas if_nametoindex must
  371. make a system call. */
  372. extern ifindex_t ifname2ifindex(const char *ifname);
  373. extern ifindex_t ifname2ifindex_vrf(const char *ifname, vrf_id_t vrf_id);
  374. /* Connected address functions. */
  375. extern struct connected *connected_new (void);
  376. extern void connected_free (struct connected *);
  377. extern void connected_add (struct interface *, struct connected *);
  378. extern struct connected *connected_add_by_prefix (struct interface *,
  379. struct prefix *,
  380. struct prefix *);
  381. extern struct connected *connected_delete_by_prefix (struct interface *,
  382. struct prefix *);
  383. extern struct connected *connected_lookup_address (struct interface *,
  384. struct in_addr);
  385. #ifndef HAVE_IF_NAMETOINDEX
  386. extern ifindex_t if_nametoindex (const char *);
  387. #endif
  388. #ifndef HAVE_IF_INDEXTONAME
  389. extern char *if_indextoname (ifindex_t, char *);
  390. #endif
  391. /* link parameters */
  392. struct if_link_params *if_link_params_get (struct interface *);
  393. void if_link_params_free (struct interface *);
  394. /* Exported variables. */
  395. extern struct list *iflist;
  396. extern struct cmd_element interface_desc_cmd;
  397. extern struct cmd_element no_interface_desc_cmd;
  398. extern struct cmd_element interface_cmd;
  399. extern struct cmd_element no_interface_cmd;
  400. extern struct cmd_element interface_vrf_cmd;
  401. extern struct cmd_element no_interface_vrf_cmd;
  402. extern struct cmd_element interface_pseudo_cmd;
  403. extern struct cmd_element no_interface_pseudo_cmd;
  404. extern struct cmd_element show_address_cmd;
  405. extern struct cmd_element show_address_vrf_cmd;
  406. extern struct cmd_element show_address_vrf_all_cmd;
  407. #endif /* _ZEBRA_IF_H */