00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "pana_session.h"
00035 #include "pana_paa_factory.h"
00036 #include "pana_database.h"
00037 #include "pana_cookie.h"
00038 #include "pana_serial_num.h"
00039 #include "pana_config_manager.h"
00040 #include "pana_sid_generator.h"
00041
00042 void PANA_PaaSessionFactory::Receive(PANA_Message &msg)
00043 {
00044
00045 AAAAvpContainer* c_sessionId = msg.avpList().search("Session-Id");
00046 if (! c_sessionId) {
00047 if (msg.flags().request) {
00048 throw (PANA_Exception(PANA_Exception::INVALID_MESSAGE,
00049 "server received invalid request message"));
00050 }
00051 switch (msg.type()) {
00052 case PANA_MTYPE_DISCOVER: ReceiveDiscover(msg); break;
00053 case PANA_MTYPE_START: ReceiveStartAnswer(msg); break;
00054 default:
00055 throw (PANA_Exception(PANA_Exception::ENTRY_NOT_FOUND,
00056 "Session Id AVP not found in packet, discarding message"));
00057 }
00058 return;
00059 }
00060 diameter_utf8string_t &sessionId = (*c_sessionId)
00061 [0]->dataRef(Type2Type<diameter_utf8string_t>());
00062
00063
00064 PANA_PaaSession *session = PANA_SESSIONDB_SEARCH(sessionId);
00065 if (session) {
00066 session->Receive(msg);
00067 return;
00068 }
00069 throw (PANA_Exception(PANA_Exception::ENTRY_NOT_FOUND,
00070 "Session not found in the database, discarding message"));
00071 }
00072
00073 void PANA_PaaSessionFactory::Error(int err)
00074 {
00075 ACE_DEBUG((LM_INFO, "(%P|%t) Server receive error handler, error=%s\n",
00076 ACE_OS_String::strerror(err)));
00077
00078
00079 }
00080
00081 void PANA_PaaSessionFactory::ReceiveDiscover(PANA_Message &msg)
00082 {
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 if (msg.tseq() != 0 || msg.rseq() != 0) {
00095 throw (PANA_Exception(PANA_Exception::INVALID_MESSAGE,
00096 "Rseq or Tseq field of discover message is non-zero"));
00097 }
00098
00099 PANA_DeviceId *ipId = msg.srcDevices().search(PANA_DeviceId::IPV4_ADDRESS);
00100 if (ipId == NULL) {
00101 throw (PANA_Exception(PANA_Exception::INVALID_MESSAGE,
00102 "Cannot determine the device ID of peer"));
00103 }
00104
00105
00106 AAAAvpContainer *c_deviceId = msg.avpList().search("Device-Id");
00107 if (c_deviceId) {
00108 PANA_TVData_t &deviceId = (*c_deviceId)[0]->dataRef
00109 (Type2Type<PANA_TVData_t>());
00110 if (deviceId.type == ipId->type()) {
00111 if (ACE_OS::memcmp(deviceId.value.data(), ipId->id().data(),
00112 deviceId.value.size())) {
00113 throw (PANA_Exception(PANA_Exception::INVALID_MESSAGE,
00114 "Device ID AVP value does not match device id of packet"));
00115 }
00116 }
00117 }
00118
00119 if (PANA_CONFIG_AUTHAGENT().use_cookie_) {
00120
00121 ACE_DEBUG((LM_INFO, "(%P|%t) RECV: Discover [stateless], S-flag %d, tseq=%d, rseq=%d\n",
00122 msg.flags().separate, msg.tseq(), msg.rseq()));
00123
00124
00125 diameter_octetstring_t cookie;
00126 PANA_COOKIE_GENERATE(const_cast<std::string&>(ipId->id()),
00127 cookie);
00128
00129 SendStartRequest(msg, cookie);
00130 }
00131 else {
00132
00133
00134
00135
00136 PANA_PaaSession *session = Create();
00137 if (session == NULL) {
00138 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00139 "Failed to auth agent session"));
00140 }
00141
00142 ACE_DEBUG((LM_INFO, "(%P|%t) New session created [stateful discovery]\n"));
00143
00144 m_PendingDb.Add(const_cast<std::string&>(ipId->id()),
00145 *session);
00146 session->Receive(msg);
00147 }
00148 }
00149
00150 void PANA_PaaSessionFactory::SendStartRequest(PANA_Message &discover,
00151 diameter_octetstring_t &cookie)
00152 {
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 AAAAvpContainerManager cm;
00196 AAAAvpContainerEntryManager em;
00197 PANA_Message *msg;
00198
00199 ACE_NEW_NORETURN(msg, PANA_Message);
00200 if (msg == NULL) {
00201 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00202 "Failed to allocate msg message"));
00203 }
00204
00205
00206 PANA_MsgHeader::Flags flg = { 1, 0, 0, 0 };
00207 msg->flags(flg);
00208 msg->type(PANA_MTYPE_START);
00209 msg->tseq(PANA_SerialNumber::generateIsn());
00210 msg->rseq(0);
00211
00212
00213 msg->flags().separate = PANA_CONFIG_AUTHAGENT().s_flag_;
00214
00215
00216 AAAAvpContainerEntry *e;
00217 if (cookie.size() > 0) {
00218
00219 AAAAvpContainer *c_cookie = cm.acquire("Cookie");
00220
00221
00222 e = em.acquire(AAA_AVPDataType(AAA_AVP_STRING_TYPE));
00223 diameter_octetstring_t &ck = e->dataRef(Type2Type<diameter_octetstring_t>());
00224 c_cookie->add(e);
00225
00226
00227 msg->avpList().add(c_cookie);
00228
00229
00230 ck.assign(cookie.data(), cookie.size());
00231 }
00232 else {
00233 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00234 "Invalid cookie generated"));
00235 }
00236
00237
00238 PANA_ProviderInfoTool infoTool;
00239 PANA_CfgProviderInfo *pInfo = &PANA_CONFIG_AUTHAGENT().nap_info_;
00240 if (pInfo->name_.length() > 0) {
00241
00242
00243 AAAAvpContainer *c_napInfo = cm.acquire("NAP-Information");
00244 e = em.acquire(AAA_AVP_GROUPED_TYPE);
00245 diameter_grouped_t &napInfo = e->dataRef(Type2Type<diameter_grouped_t>());
00246 c_napInfo->add(e);
00247
00248
00249 msg->avpList().add(c_napInfo);
00250 infoTool.Add(napInfo, *pInfo);
00251 }
00252 else {
00253 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00254 "NAP Information missing"));
00255 }
00256
00257
00258 PANA_CfgProviderList &ispList = PANA_CONFIG_AUTHAGENT().isp_info_;
00259 if (ispList.size() > 0) {
00260
00261
00262 AAAAvpContainer *c_ispInfo = cm.acquire("ISP-Information");
00263 msg->avpList().add(c_ispInfo);
00264
00265 PANA_CfgProviderList::iterator i;
00266 for (i = ispList.begin(); i != ispList.end(); i ++) {
00267
00268 pInfo = (*i);
00269
00270
00271 e = em.acquire(AAA_AVP_GROUPED_TYPE);
00272 diameter_grouped_t &ispInfo = e->dataRef(Type2Type<diameter_grouped_t>());
00273 c_ispInfo->add(e);
00274
00275
00276 PANA_ProviderInfoTool info;
00277 info.Add(ispInfo, *pInfo);
00278 }
00279 }
00280
00281
00282 PANA_DeviceId *ipId = discover.srcDevices().search(PANA_DeviceId::IPV4_ADDRESS);
00283 msg->srcDevices().move(discover.srcDevices(), *ipId);
00284
00285
00286 msg->srcPort(discover.srcPort());
00287
00288 m_UdpChannel.Send(*msg);
00289
00290 ACE_DEBUG((LM_INFO, "(%P|%t) SEND: Start-Request, tseq=%d, rseq=%d\n",
00291 msg->tseq(), msg->rseq()));
00292 }
00293
00294 void PANA_PaaSessionFactory::ReceiveStartAnswer(PANA_Message &msg)
00295 {
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 if (msg.tseq() == 0 || msg.rseq() == 0) {
00337 throw (PANA_Exception(PANA_Exception::INVALID_MESSAGE,
00338 "Rseq or Tseq field of start answer message is zero"));
00339 }
00340
00341 ACE_DEBUG((LM_INFO, "(%P|%t) RECV: Start answer [factory], S-flag %d, tseq=%d, rseq=%d\n",
00342 msg.flags().separate, msg.tseq(), msg.rseq()));
00343
00344 PANA_PaaSession *session = NULL;
00345 if (PANA_CONFIG_AUTHAGENT().use_cookie_) {
00346
00347 PANA_Paa::ValidateCookie(msg);
00348
00349 ACE_DEBUG((LM_INFO, "(%P|%t) Pac validated, creating new session\n"));
00350 session = Create();
00351 }
00352 else {
00353 PANA_DeviceId *ipId = msg.srcDevices().search(PANA_DeviceId::IPV4_ADDRESS);
00354 if (ipId == NULL) {
00355 throw (PANA_Exception(PANA_Exception::INVALID_MESSAGE,
00356 "Cannot determine the device ID of peer"));
00357 }
00358
00359 ACE_DEBUG((LM_INFO, "(%P|%t) Stateful discovery, updating session\n"));
00360 m_PendingDb.Remove(const_cast<std::string&>(ipId->id()),
00361 &session);
00362 }
00363
00364 if (session == NULL) {
00365 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00366 "Failed to auth agent session"));
00367 }
00368
00369
00370 diameter_octetstring_t sessionId;
00371 PANA_SessionIdGenerator_S::instance()->Generate(sessionId);
00372
00373 PANA_SESSIONDB_ADD(sessionId, *session);
00374
00375
00376 AAAAvpContainerManager cm;
00377 AAAAvpContainerEntryManager em;
00378 AAAAvpContainer *c_sessionId = cm.acquire("Session-Id");
00379
00380
00381 AAAAvpContainerEntry *e = em.acquire(AAA_AVP_UTF8_STRING_TYPE);
00382 diameter_utf8string_t &sid = e->dataRef(Type2Type<diameter_utf8string_t>());
00383 c_sessionId->add(e);
00384
00385 msg.avpList().add(c_sessionId);
00386 sid = sessionId;
00387
00388 session->Receive(msg);
00389 }
00390
00391
00392
00393
00394