File: LinkControlProtocolP.nc

package info (click to toggle)
tinyos 2.1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 47,476 kB
  • ctags: 36,607
  • sloc: ansic: 63,646; cpp: 14,974; java: 10,358; python: 5,215; makefile: 1,724; sh: 902; asm: 597; xml: 392; perl: 74; awk: 46
file content (446 lines) | stat: -rw-r--r-- 18,102 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* Copyright (c) 2010 People Power Co.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the People Power Corporation nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * PEOPLE POWER CO. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE
 *
 */

#include "ppp.h"
#include "lcp.h"
#include "LcpAutomaton.h"

module LinkControlProtocolP {
  provides {
    interface PppProtocol;
    interface PppProtocolCodeSupport as ProtocolReject;
    interface PppProtocolOption as AddressControlFieldCompressionOption;
#if PPP_LCP_ENABLE_PCOMP
    interface PppProtocolOption as ProtocolFieldCompressionOption;
#endif /* PPP_LCP_ENABLE_PCOMP */
    interface PppProtocolOption as MaximumReceiveUnitOption;
    interface PppProtocolOption as AsyncControlCharacterMapOption;
  }
  uses {
    interface Ppp;
    interface PppProtocolCodeCoordinator;
    interface GetSetOptions<HdlcFramingOptions_t> as HdlcFramingOptions;
    interface GetSetOptions<PppOptions_t> as PppOptions;
    interface PppRejectedProtocol;
    interface LcpAutomaton;
  }
  enum {
    Protocol = PppProtocol_LinkControlProtocol,
  };
} implementation {
  
  /** Compressed booleans to identify those options for which
   * negotiation is permitted. */
  struct {
      uint8_t accomp:1;
      uint8_t pcomp:1;
      uint8_t mru:1;
      uint8_t accm:1;
  } isNegotiable = { accomp : TRUE,
                     pcomp : TRUE,
                     mru : TRUE,
                     accm : TRUE };

  event void Ppp.outputFrameTransmitted (frame_key_t key,
                                         error_t err) { }

  command unsigned int PppProtocol.getProtocol () { return Protocol; }

  command error_t PppProtocol.rejectedByPeer (const uint8_t* data,
                                              const uint8_t* data_end)
  {
    /* Not sure what to do about this, since the peer could only send
     * a Protocol-Reject message if it actually implemented the
     * Link-Control-Protocol. */
    return SUCCESS;
  }

  event void LcpAutomaton.transitionCompleted (LcpAutomatonState_e state) { }
  event void LcpAutomaton.thisLayerUp () { }
  event void LcpAutomaton.thisLayerDown () { }
  event void LcpAutomaton.thisLayerStarted () { }
  event void LcpAutomaton.thisLayerFinished () { }

  /* ============================== */
  /* AddressControlFieldCompression */

  /* ACCOMP is sent in a request to indicate that local can receive
   * frames without adddress and control fields.  Ack transmission
   * (remote) acknowledges permission to suppress ACF in transmitted
   * packets.  Ack reception (local) has no meaning, as we need to be
   * able to interpret packets with and without compression. */
  command uint8_t AddressControlFieldCompressionOption.getType () { return LCPOpt_AddressControlFieldCompression; }
  command bool AddressControlFieldCompressionOption.isNegotiable () { return isNegotiable.accomp; }
  command void AddressControlFieldCompressionOption.setNegotiable (bool is_negotiable) { isNegotiable.accomp = is_negotiable; }
  command uint8_t AddressControlFieldCompressionOption.considerRequest (const uint8_t* dp,
                                                                        const uint8_t* dpe)
  {
    return PppControlProtocolCode_ConfigureAck;
  }
  command uint8_t* AddressControlFieldCompressionOption.appendRequest (uint8_t* dp,
                                                                       const uint8_t* dpe)
  { return dp; }
  command uint8_t* AddressControlFieldCompressionOption.appendNakValue (const uint8_t* sp,
                                                                        const uint8_t* spe,
                                                                        uint8_t* dp,
                                                                        const uint8_t* dpe)
  { return dp; }
  command void AddressControlFieldCompressionOption.setRemote (const uint8_t* dp,
                                                                  const uint8_t* dpe)
  { 
    HdlcFramingOptions_t hdlcopt;
    /* We have accepted that the remote can receive frames with
     * address and control fields compressed.  Suppress them on
     * transmission. */
    hdlcopt = call HdlcFramingOptions.get();
    hdlcopt.txSuppressAddressControl = (0 != dp);
    call HdlcFramingOptions.set(&hdlcopt);
  }
  command void AddressControlFieldCompressionOption.setLocal (const uint8_t* dp,
                                                                 const uint8_t* dpe)
  {
    HdlcFramingOptions_t hdlcopt;
    /* The peer has accepted that we can receive frames with address
     * and control fields compressed.  Suppress them on reception. */
    hdlcopt = call HdlcFramingOptions.get();
    hdlcopt.rxSuppressAddressControl = (0 != dp);
    call HdlcFramingOptions.set(&hdlcopt);
  }
  command void AddressControlFieldCompressionOption.processNakValue (const uint8_t* dp,
                                                                     const uint8_t* dpe)
  {
    call AddressControlFieldCompressionOption.setNegotiable(FALSE);
  }
  command void AddressControlFieldCompressionOption.reset ()
  {
    call AddressControlFieldCompressionOption.setNegotiable(TRUE);
  }

#if PPP_LCP_ENABLE_PCOMP
  /* ======================== */
  /* ProtocolFieldCompression */

  /* PCOMP is sent in a request to indicate that local can receive
   * frames without adddress and control fields.  Ack transmission
   * (remote) acknowledges permission to compress protocols in
   * transmitted packets.  Ack reception (local) has no meaning, as we
   * need to be able to interpret packets with and without
   * compression. */
  command uint8_t ProtocolFieldCompressionOption.getType () { return LCPOpt_ProtocolFieldCompression; }
  command bool ProtocolFieldCompressionOption.isNegotiable () { return isNegotiable.pcomp; }
  command void ProtocolFieldCompressionOption.setNegotiable (bool is_negotiable) { isNegotiable.pcomp = is_negotiable; }
  command uint8_t ProtocolFieldCompressionOption.considerRequest (const uint8_t* dp,
                                                                        const uint8_t* dpe)
  {
    return PppControlProtocolCode_ConfigureAck;
  }
  command uint8_t* ProtocolFieldCompressionOption.appendRequest (uint8_t* dp,
                                                                       const uint8_t* dpe)
  { return dp; }
  command uint8_t* ProtocolFieldCompressionOption.appendNakValue (const uint8_t* sp,
                                                                  const uint8_t* spe,
                                                                  uint8_t* dp,
                                                                  const uint8_t* dpe)
  { return dp; }
  command void ProtocolFieldCompressionOption.setRemote (const uint8_t* dp,
                                                            const uint8_t* dpe)
  { 
    PppOptions_t opt;
    /* We have accepted that the remote can receive frames with the
     * protocol field compressed.  Compress it on transmission. */
    opt = call PppOptions.get();
    opt.txProtocolFieldCompression = (0 != dp);
    call PppOptions.set(&opt);
  }
  command void ProtocolFieldCompressionOption.setLocal (const uint8_t* dp,
                                                           const uint8_t* dpe)
  {
    PppOptions_t opt;
    /* The peer has accepted that we can receive frames with the
     * protocol field compressed.  Try reading them that way on
     * reception. */
    opt = call PppOptions.get();
    opt.rxProtocolFieldCompression = (0 != dp);
    call PppOptions.set(&opt);
  }
  command void ProtocolFieldCompressionOption.processNakValue (const uint8_t* dp,
                                                               const uint8_t* dpe)
  {
    call ProtocolFieldCompressionOption.setNegotiable(FALSE);
  }
  command void ProtocolFieldCompressionOption.reset ()
  {
    call ProtocolFieldCompressionOption.setNegotiable(TRUE);
  }
#endif /* PPP_LCP_ENABLE_PCOMP */

  /* ================== */
  /* MaximumReceiveUnit */

  enum {
    PPP_DefaultMRU = 1500,
  };
  uint16_t proposedMRU_ = PPP_PREFERRED_MRU;

  /* MRU is sent in a request to indicate that local can receive
   * frames with information fields that exceed the PPP default, or to
   * request that the remote transmit shorter frames.  Ack
   * transmission (remote) indicates a promise to transmit no more
   * than the MRU in each frame.  Ack reception (local) indicates we
   * might not receive messages larger than the MRU. */
  command uint8_t MaximumReceiveUnitOption.getType () { return LCPOpt_MaximumReceiveUnit; }
  command bool MaximumReceiveUnitOption.isNegotiable ()
  {
    PppOptions_t opt = call PppOptions.get();
    return isNegotiable.mru && (PPP_DefaultMRU != opt.rxMaximumReceiveUnit);
  }
  command void MaximumReceiveUnitOption.setNegotiable (bool is_negotiable) { isNegotiable.mru = is_negotiable; }
  command uint8_t MaximumReceiveUnitOption.considerRequest (const uint8_t* dp,
                                                            const uint8_t* dpe)
  {
    /* Although we may not be able to support the default MRU on
     * reception, there's no facility to force the remote to negotiate
     * the MRU option.  Best we can do is, if they sent it, make sure
     * it's acceptable: i.e., doesn't exceed the maximum, or is what
     * we proposed. */
    uint16_t tx_mru = (dp[0] << 8) | dp[1];
    if ((tx_mru > PPP_MAXIMUM_MRU) && (proposedMRU_ != tx_mru)) {
      return PppControlProtocolCode_ConfigureNak;
    }
    return PppControlProtocolCode_ConfigureAck;
  }
  command uint8_t* MaximumReceiveUnitOption.appendRequest (uint8_t* dp,
                                                           const uint8_t* dpe)
  {
    if (dpe < (dp+2)) {
      return 0;
    }
    *dp++ = (proposedMRU_ >> 8);
    *dp++ = (proposedMRU_ & 0x0FF);
    return dp;
  }
  command uint8_t* MaximumReceiveUnitOption.appendNakValue (const uint8_t* sp,
                                                            const uint8_t* spe,
                                                            uint8_t* dp,
                                                            const uint8_t* dpe)
  {
    if (dpe < (dp+2)) {
      return 0;
    }
    *dp++ = (PPP_PREFERRED_MRU >> 8);
    *dp++ = (PPP_PREFERRED_MRU & 0x0FF);
    return dp;
  }
  command void MaximumReceiveUnitOption.setRemote (const uint8_t* dp,
                                                      const uint8_t* dpe)
  { 
    PppOptions_t opt;
    uint16_t value;

    /* We have accepted the size requested by the remote.  Limit
     * transmissions to that size. */
    opt = call PppOptions.get();
    if (dp) {
      value = (dp[0] << 8) | dp[1];
    } else {
      value = PPP_PREFERRED_MRU;
    }
    opt.txMaximumReceiveUnit = value;
    call PppOptions.set(&opt);
  }
  command void MaximumReceiveUnitOption.setLocal (const uint8_t* dp,
                                                     const uint8_t* dpe)
  {
    PppOptions_t opt;
    uint16_t value;

    /* The peer has accepted our preferred size.  Configure receptions
     * to expect it. */
    opt = call PppOptions.get();
    if (dp) {
      value = (dp[0] << 8) | dp[1];
    } else {
      value = PPP_PREFERRED_MRU;
    }
    opt.rxMaximumReceiveUnit = value;
    call PppOptions.set(&opt);
  }
  command void MaximumReceiveUnitOption.processNakValue (const uint8_t* dp,
                                                         const uint8_t* dpe)
  {
    proposedMRU_ = (dp[0] << 8) | dp[1];
  }
  command void MaximumReceiveUnitOption.reset ()
  {
    call MaximumReceiveUnitOption.setNegotiable(TRUE);
    proposedMRU_ = PPP_PREFERRED_MRU;
  }

  /* ================== */
  /* AsyncControlCharacterMap */

  enum {
    HDLC_DefaultACCM = ~0UL,
    HDLC_PreferredACCM = 0UL,
  };
  uint32_t proposedACCM_;

  /* ACCM is sent in a request to indicate that local does not require
   * certain control characters to be mapped for transparency.  Ack
   * transmission (remote) indicates a promise to escape the characters
   * specified in the ACCM.  Ack reception (local) indicates that we
   * can expect the characters not marked in the map to be received
   * unescaped.
   *
   * Local is reset prior to SCR.  On local reset, restore both rxSCCM
   * and txSCCM to defaults.
   *
   * Local is set on CA reception.  On local set, set rxACCM.
   *
   * Remote is set after CA transmission.  On remote set, set txACCM.
   *
   * When is remote reset?
   */
  command uint8_t AsyncControlCharacterMapOption.getType () { return LCPOpt_AsyncControlCharacterMap; }
  command bool AsyncControlCharacterMapOption.isNegotiable ()
  {
    return isNegotiable.accm;
  }
  command void AsyncControlCharacterMapOption.setNegotiable (bool is_negotiable) { isNegotiable.accm = is_negotiable; }
  command uint8_t AsyncControlCharacterMapOption.considerRequest (const uint8_t* dp,
                                                                  const uint8_t* dpe)
  {
    return PppControlProtocolCode_ConfigureAck;
  }
  command uint8_t* AsyncControlCharacterMapOption.appendRequest (uint8_t* dp,
                                                           const uint8_t* dpe)
  {
    nx_uint32_t* np = (nx_uint32_t*)dp;
    if (dpe < (dp+1)) {
      return 0;
    }
    *np = proposedACCM_;
    return (uint8_t*)(np+1);
  }
  command uint8_t* AsyncControlCharacterMapOption.appendNakValue (const uint8_t* sp,
                                                                  const uint8_t* spe,
                                                                  uint8_t* dp,
                                                                  const uint8_t* dpe)
  {
    nx_uint32_t* np = (nx_uint32_t*)dp;
    if (dpe < (dp+1)) {
      return 0;
    }
    *np = HDLC_PreferredACCM;
    return (uint8_t*)(np+1);
  }
  command void AsyncControlCharacterMapOption.setRemote (const uint8_t* dp,
                                                         const uint8_t* dpe)
  { 
    HdlcFramingOptions_t opt;
    uint32_t value;

    /* We have accepted the map requested by the remote.  Perform
     * transparency operations on characters in that map. */
    opt = call HdlcFramingOptions.get();
    if (dp) {
      value = *(nx_uint32_t*)dp;
    } else {
      value = HDLC_DefaultACCM;
    }
    opt.txAsyncControlCharacterMap = value;
    call HdlcFramingOptions.set(&opt);
  }
  command void AsyncControlCharacterMapOption.setLocal (const uint8_t* dp,
                                                        const uint8_t* dpe)
  {
    HdlcFramingOptions_t opt;
    uint32_t value;

    /* The peer has accepted our proposed map.  This has no effect on
     * our behavior. */
    opt = call HdlcFramingOptions.get();
    if (dp) {
      value = *(nx_uint32_t*)dp;
    } else {
      value = HDLC_DefaultACCM;
    }
    opt.rxAsyncControlCharacterMap = value;
    call HdlcFramingOptions.set(&opt);
  }
  command void AsyncControlCharacterMapOption.processNakValue (const uint8_t* dp,
                                                               const uint8_t* dpe)
  {
    proposedACCM_ = *(nx_uint32_t*)dp;
  }
  command void AsyncControlCharacterMapOption.reset ()
  {
    call AsyncControlCharacterMapOption.setNegotiable(TRUE);
    proposedACCM_ = HDLC_PreferredACCM;
  }

  /* ======================================== */

  command uint8_t ProtocolReject.getCode () { return PppControlProtocolCode_ProtocolReject; }
  command error_t ProtocolReject.process (uint8_t identifier,
                                          const uint8_t* data,
                                          const uint8_t* data_end)
  {
    uint16_t protocol = 0;
    protocol = (data[0] << 8) + data[1];
    return call PppRejectedProtocol.rejected(protocol, data, data_end);
  }

  /** Invoke some code-specific operation */
  command error_t ProtocolReject.invoke (void* param_,
                                         frame_key_t* keyp)
  {
    error_t rc = EINVAL;
    protocolReject_param_t* param = (protocolReject_param_t*)param_;

    if (LAS_Opened == call LcpAutomaton.getState()) {
      /* Due to implementation choice, we can't reject a protocol value
       * of zero, which is fine since that's an illegal protocol value.
       * Just drop those on the floor. */
      if (0 != param->protocol) {
        rc = call PppProtocolCodeCoordinator.rejectPacket(param->protocol, param->information, param->information + param->information_length, keyp);
      }
    }
    return rc;
  }

  command error_t PppProtocol.process (const uint8_t* information,
                                       unsigned int information_length)
  {
    return call PppProtocolCodeCoordinator.dispatch(information, information_length);
  }

}