1 /* 2 * Copyright 2003-2006, Haiku Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #ifndef _K_PPP_INTERFACE__H 7 #define _K_PPP_INTERFACE__H 8 9 #include <driver_settings.h> 10 11 #ifndef _K_PPP_DEFS__H 12 #include <KPPPDefs.h> 13 #endif 14 15 #ifndef _PPP_CONTROL__H 16 #include <PPPControl.h> 17 #endif 18 19 #ifndef _K_PPP_LCP__H 20 #include <KPPPLCP.h> 21 #endif 22 23 #ifndef _K_PPP_REPORT_MANAGER__H 24 #include <KPPPReportManager.h> 25 #endif 26 27 #ifndef _K_PPP_STATE_MACHINE__H 28 #include <KPPPStateMachine.h> 29 #endif 30 31 #include <TemplateList.h> 32 33 #include <net_buffer.h> 34 #include <net_device.h> 35 36 #include <lock.h> 37 #include <util/AutoLock.h> 38 39 class KPPPDevice; 40 class KPPPProtocol; 41 class KPPPOptionHandler; 42 43 struct ppp_interface_module_info; 44 struct ppp_module_info; 45 struct ppp_interface_entry; 46 47 48 class KPPPInterface : public KPPPLayer { 49 friend class PPPManager; 50 friend class KPPPStateMachine; 51 52 private: 53 //! Copies are not allowed. 54 KPPPInterface(const KPPPInterface& copy); 55 KPPPInterface& operator= (const KPPPInterface& copy); 56 57 public: 58 // we should set to private after finishing test 59 // only PPPManager may construct us! 60 KPPPInterface(const char *name, ppp_interface_entry *entry, 61 ppp_interface_id ID, const driver_settings *settings, 62 KPPPInterface *parent = NULL); 63 ~KPPPInterface(); 64 65 public: 66 void Delete(); 67 68 virtual status_t InitCheck() const; 69 70 //! Returns this interface's unique identifier. ID()71 ppp_interface_id ID() const 72 { return fID; } 73 74 //! Returns interface's settings. Settings()75 driver_settings* Settings() const 76 { return fSettings; } 77 78 //! Returns the KPPPStateMachine of this interface. StateMachine()79 KPPPStateMachine& StateMachine() 80 { return fStateMachine; } 81 //! Returns the KPPPLCP protocol of this interface. LCP()82 KPPPLCP& LCP() 83 { return fLCP; } 84 85 //! Returns the interfac's ifnet structure that is exported to the netstack. Ifnet()86 net_device *Ifnet() const 87 { return fIfnet; } 88 89 const char *Username() const; 90 const char *Password() const; 91 92 //! Delay in miliseconds between a connect retry. ConnectRetryDelay()93 uint32 ConnectRetryDelay() const 94 { return fConnectRetryDelay; } 95 //! Delay in miliseconds to wait until reconnecting. ReconnectDelay()96 uint32 ReconnectDelay() const 97 { return fReconnectDelay; } 98 99 //! Time when connection was established successfully. ConnectedSince()100 bigtime_t ConnectedSince() const 101 { return fConnectedSince; } 102 103 //! Used for reporting that a packet was send/received (updates idle time). UpdateIdleSince()104 void UpdateIdleSince() 105 { fUpdateIdleSince = true; } 106 //! Returns the time in seconds when the last packet was received. IdleSince()107 uint32 IdleSince() const 108 { return fIdleSince; } 109 //! If no packets were received this number of seconds we will disconnect. DisconnectAfterIdleSince()110 uint32 DisconnectAfterIdleSince() const 111 { return fDisconnectAfterIdleSince; } 112 113 bool SetMRU(uint32 MRU); 114 //! This is the smallest MRU that we and the peer have. MRU()115 uint32 MRU() const 116 { return fMRU; } 117 //! Sets interface's maximum transfer unit (will set ifnet value accordingly). SetInterfaceMTU(uint32 interfaceMTU)118 bool SetInterfaceMTU(uint32 interfaceMTU) 119 { return SetMRU(interfaceMTU - fHeaderLength); } 120 //! This is the MRU including protocol overhead. InterfaceMTU()121 uint32 InterfaceMTU() const 122 { return fInterfaceMTU; } 123 //! Includes the length of all device and encapsulator headers. 124 uint32 PacketOverhead() const; 125 126 virtual status_t Control(uint32 op, void *data, size_t length); 127 128 bool SetDevice(KPPPDevice *device); 129 //! Returns interface's transport device. Device()130 KPPPDevice *Device() const 131 { return fDevice; } 132 133 bool AddProtocol(KPPPProtocol *protocol); 134 bool RemoveProtocol(KPPPProtocol *protocol); 135 int32 CountProtocols() const; 136 KPPPProtocol *ProtocolAt(int32 index) const; 137 //! Returns first protocol in chain. FirstProtocol()138 KPPPProtocol *FirstProtocol() const 139 { return fFirstProtocol; } 140 KPPPProtocol *ProtocolFor(uint16 protocolNumber, 141 KPPPProtocol *start = NULL) const; 142 143 // multilink methods 144 bool AddChild(KPPPInterface *child); 145 bool RemoveChild(KPPPInterface *child); 146 //! Returns the number of child interfaces that this interface has. CountChildren()147 int32 CountChildren() const 148 { return fChildren.CountItems(); } 149 KPPPInterface *ChildAt(int32 index) const; 150 //! Returns this interface's parent. Parent()151 KPPPInterface *Parent() const 152 { return fParent; } 153 //! Returns whether we are a multilink-interface. IsMultilink()154 bool IsMultilink() const 155 { return fIsMultilink; } 156 157 void SetAutoReconnect(bool autoReconnect = true); 158 //! Returns whether this interface reconnects automatically. DoesAutoReconnect()159 bool DoesAutoReconnect() const 160 { return fAutoReconnect; } 161 162 void SetConnectOnDemand(bool connectOnDemand = true); 163 //! Returns whether connect-on-demand (auto-connect) is enabled. DoesConnectOnDemand()164 bool DoesConnectOnDemand() const 165 { return fConnectOnDemand; } 166 167 void SetAskBeforeConnecting(bool ask); 168 //! Returns whether the user is asked before establishing the connection. DoesAskBeforeConnecting()169 bool DoesAskBeforeConnecting() const 170 { return fAskBeforeConnecting; } 171 172 //! Clients are in \c PPP_CLIENT_MODE and servers are in \c PPP_SERVER_MODE. Mode()173 ppp_mode Mode() const 174 { return fMode; } 175 //! Current state of the state machine (see enum: \c ppp_state). State()176 ppp_state State() const 177 { return fStateMachine.State(); } 178 //! Current phase of the state machine (see enum: \c ppp_phase). Phase()179 ppp_phase Phase() const 180 { return fStateMachine.Phase(); } 181 182 // Protocol-Field-Compression 183 bool SetPFCOptions(uint8 pfcOptions); 184 //! PFC option flags as defined in enum: \c ppp_pfc_options. PFCOptions()185 uint8 PFCOptions() const 186 { return fPFCOptions; } 187 /*! \brief Local PFC state 188 189 Values defined in \c ppp_pfc_state. \n 190 The local PFC state says if we accepted a request from the peer 191 i.e.: we may use PFC in outgoing packets. 192 */ LocalPFCState()193 ppp_pfc_state LocalPFCState() const 194 { return fLocalPFCState; } 195 /*! \brief Peer PFC state 196 197 Values defined in \c ppp_pfc_state. \n 198 The peer PFC state says if the peer accepted a request us 199 i.e.: the peer might send PFC-compressed packets to us 200 */ PeerPFCState()201 ppp_pfc_state PeerPFCState() const 202 { return fPeerPFCState; } 203 //! Shortcut for check if local state is \c PPP_PFC_ACCEPTED. UseLocalPFC()204 bool UseLocalPFC() const 205 { return LocalPFCState() == PPP_PFC_ACCEPTED; } 206 207 virtual bool Up(); 208 // in server mode Up() listens for an incoming connection 209 virtual bool Down(); 210 bool WaitForConnection(); 211 //! Returns if the interface is connected. IsUp()212 bool IsUp() const 213 { return Phase() == PPP_ESTABLISHED_PHASE; } 214 215 //! Returns interface's report manager. ReportManager()216 KPPPReportManager& ReportManager() 217 { return fReportManager; } 218 //! Shortcut to KPPPReportManager::Report() of this interface's report manager. Report(ppp_report_type type,int32 code,void * data,int32 length)219 bool Report(ppp_report_type type, int32 code, void *data, int32 length) 220 { return ReportManager().Report(type, code, data, length); } 221 // returns false if reply was bad (or an error occured) 222 223 bool LoadModules(driver_settings *settings, 224 int32 start, int32 count); 225 bool LoadModule(const char *name, driver_parameter *parameter, 226 ppp_module_key_type type); 227 228 virtual bool IsAllowedToSend() const; 229 // always returns true 230 231 virtual status_t Send(net_buffer *packet, uint16 protocolNumber); 232 // sends the packet to the next handler (normally the device) 233 virtual status_t Receive(net_buffer *packet, uint16 protocolNumber); 234 235 status_t ReceiveFromDevice(net_buffer *packet); 236 // This must not block KPPPDevice::Send()! 237 238 void Pulse(); 239 // this manages all timeouts, etc. 240 241 private: 242 bool RegisterInterface(); 243 // adds us to the manager module and 244 // saves the returned ifnet structure 245 bool UnregisterInterface(); 246 247 status_t StackControl(uint32 op, void *data); 248 // stack routes ioctls to interface 249 status_t StackControlEachHandler(uint32 op, void *data); 250 // this calls StackControl() with the given parameters for each handler 251 252 void CalculateInterfaceMTU(); 253 void CalculateBaudRate(); 254 255 void Reconnect(uint32 delay); 256 257 // multilink methods 258 //! Set the parent of this interface. SetParent(KPPPInterface * parent)259 void SetParent(KPPPInterface *parent) 260 { fParent = parent; } 261 262 private: 263 ppp_interface_id fID; 264 // the manager assigns an ID to every interface 265 driver_settings *fSettings; 266 net_device *fIfnet; 267 268 char *fUsername, *fPassword; 269 270 thread_id fReconnectThread; 271 uint32 fConnectAttempt, fConnectRetriesLimit; 272 uint32 fConnectRetryDelay, fReconnectDelay; 273 274 ppp_interface_module_info *fManager; 275 276 ppp_statistics fStatistics; 277 bigtime_t fConnectedSince; 278 uint32 fIdleSince, fDisconnectAfterIdleSince; 279 bool fUpdateIdleSince; 280 uint32 fMRU, fInterfaceMTU, fHeaderLength; 281 282 KPPPInterface *fParent; 283 TemplateList<KPPPInterface*> fChildren; 284 bool fIsMultilink; 285 286 bool fAutoReconnect, fConnectOnDemand, fAskBeforeConnecting; 287 288 ppp_mode fMode; 289 ppp_pfc_state fLocalPFCState, fPeerPFCState; 290 uint8 fPFCOptions; 291 292 KPPPDevice *fDevice; 293 KPPPProtocol *fFirstProtocol; 294 TemplateList<char*> fModules; 295 296 KPPPStateMachine fStateMachine; 297 KPPPLCP fLCP; 298 KPPPReportManager fReportManager; 299 mutex& fLock; 300 int32 fDeleteCounter; 301 }; 302 303 304 #endif 305