1cb9cadadSEd Warnicke/*
2cb9cadadSEd Warnicke * Copyright (c) 2015 Cisco and/or its affiliates.
3cb9cadadSEd Warnicke * Licensed under the Apache License, Version 2.0 (the "License");
4cb9cadadSEd Warnicke * you may not use this file except in compliance with the License.
5cb9cadadSEd Warnicke * You may obtain a copy of the License at:
6cb9cadadSEd Warnicke *
7cb9cadadSEd Warnicke *     http://www.apache.org/licenses/LICENSE-2.0
8cb9cadadSEd Warnicke *
9cb9cadadSEd Warnicke * Unless required by applicable law or agreed to in writing, software
10cb9cadadSEd Warnicke * distributed under the License is distributed on an "AS IS" BASIS,
11cb9cadadSEd Warnicke * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb9cadadSEd Warnicke * See the License for the specific language governing permissions and
13cb9cadadSEd Warnicke * limitations under the License.
14cb9cadadSEd Warnicke */
15cb9cadadSEd Warnicke/*
16cb9cadadSEd Warnicke * ppp_pg.c: packet generator ppp interface
17cb9cadadSEd Warnicke *
18cb9cadadSEd Warnicke * Copyright (c) 2008 Eliot Dresselhaus
19cb9cadadSEd Warnicke *
20cb9cadadSEd Warnicke * Permission is hereby granted, free of charge, to any person obtaining
21cb9cadadSEd Warnicke * a copy of this software and associated documentation files (the
22cb9cadadSEd Warnicke * "Software"), to deal in the Software without restriction, including
23cb9cadadSEd Warnicke * without limitation the rights to use, copy, modify, merge, publish,
24cb9cadadSEd Warnicke * distribute, sublicense, and/or sell copies of the Software, and to
25cb9cadadSEd Warnicke * permit persons to whom the Software is furnished to do so, subject to
26cb9cadadSEd Warnicke * the following conditions:
27cb9cadadSEd Warnicke *
28cb9cadadSEd Warnicke * The above copyright notice and this permission notice shall be
29cb9cadadSEd Warnicke * included in all copies or substantial portions of the Software.
30cb9cadadSEd Warnicke *
31cb9cadadSEd Warnicke *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32cb9cadadSEd Warnicke *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33cb9cadadSEd Warnicke *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34cb9cadadSEd Warnicke *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35cb9cadadSEd Warnicke *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36cb9cadadSEd Warnicke *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37cb9cadadSEd Warnicke *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38cb9cadadSEd Warnicke */
39cb9cadadSEd Warnicke
40cb9cadadSEd Warnicke#include <vlib/vlib.h>
41cb9cadadSEd Warnicke#include <vnet/pg/pg.h>
42cb9cadadSEd Warnicke#include <vnet/ppp/ppp.h>
43cb9cadadSEd Warnicke
44aba0fe49SCalvintypedef struct
45aba0fe49SCalvin{
46cb9cadadSEd Warnicke  pg_edit_t address;
47cb9cadadSEd Warnicke  pg_edit_t control;
48cb9cadadSEd Warnicke  pg_edit_t protocol;
49cb9cadadSEd Warnicke} pg_ppp_header_t;
50cb9cadadSEd Warnicke
51cb9cadadSEd Warnickestatic inline void
52cb9cadadSEd Warnickepg_ppp_header_init (pg_ppp_header_t * e)
53cb9cadadSEd Warnicke{
54cb9cadadSEd Warnicke  pg_edit_init (&e->address, ppp_header_t, address);
55cb9cadadSEd Warnicke  pg_edit_init (&e->control, ppp_header_t, control);
56cb9cadadSEd Warnicke  pg_edit_init (&e->protocol, ppp_header_t, protocol);
57cb9cadadSEd Warnicke}
58cb9cadadSEd Warnicke
59cb9cadadSEd Warnickeuword
60cb9cadadSEd Warnickeunformat_pg_ppp_header (unformat_input_t * input, va_list * args)
61cb9cadadSEd Warnicke{
62aba0fe49SCalvin  pg_stream_t *s = va_arg (*args, pg_stream_t *);
63aba0fe49SCalvin  pg_ppp_header_t *h;
64cb9cadadSEd Warnicke  u32 group_index, error;
65aba0fe49SCalvin
66cb9cadadSEd Warnicke  h = pg_create_edit_group (s, sizeof (h[0]), sizeof (ppp_header_t),
67cb9cadadSEd Warnicke			    &group_index);
68cb9cadadSEd Warnicke  pg_ppp_header_init (h);
69cb9cadadSEd Warnicke
70cb9cadadSEd Warnicke  pg_edit_set_fixed (&h->address, 0xff);
71cb9cadadSEd Warnicke  pg_edit_set_fixed (&h->control, 0x03);
72cb9cadadSEd Warnicke
73cb9cadadSEd Warnicke  error = 1;
74aba0fe49SCalvin  if (!unformat (input, "%U",
75aba0fe49SCalvin		 unformat_pg_edit,
76aba0fe49SCalvin		 unformat_ppp_protocol_net_byte_order, &h->protocol))
77cb9cadadSEd Warnicke    goto done;
78cb9cadadSEd Warnicke
79cb9cadadSEd Warnicke  {
80aba0fe49SCalvin    ppp_main_t *pm = &ppp_main;
81aba0fe49SCalvin    ppp_protocol_info_t *pi = 0;
82aba0fe49SCalvin    pg_node_t *pg_node = 0;
83cb9cadadSEd Warnicke
84cb9cadadSEd Warnicke    if (h->protocol.type == PG_EDIT_FIXED)
85cb9cadadSEd Warnicke      {
86cb9cadadSEd Warnicke	u16 t = *(u16 *) h->protocol.values[PG_EDIT_LO];
87cb9cadadSEd Warnicke	pi = ppp_get_protocol_info (pm, clib_net_to_host_u16 (t));
88cb9cadadSEd Warnicke	if (pi && pi->node_index != ~0)
89cb9cadadSEd Warnicke	  pg_node = pg_get_node (pi->node_index);
90cb9cadadSEd Warnicke      }
91cb9cadadSEd Warnicke
92cb9cadadSEd Warnicke    if (pg_node && pg_node->unformat_edit
93cb9cadadSEd Warnicke	&& unformat_user (input, pg_node->unformat_edit, s))
94cb9cadadSEd Warnicke      ;
95cb9cadadSEd Warnicke
96aba0fe49SCalvin    else if (!unformat_user (input, unformat_pg_payload, s))
97cb9cadadSEd Warnicke      goto done;
98cb9cadadSEd Warnicke  }
99cb9cadadSEd Warnicke
100cb9cadadSEd Warnicke  error = 0;
101aba0fe49SCalvindone:
102cb9cadadSEd Warnicke  if (error)
103cb9cadadSEd Warnicke    pg_free_edit_group (s);
104cb9cadadSEd Warnicke  return error == 0;
105cb9cadadSEd Warnicke}
106cb9cadadSEd Warnicke
107aba0fe49SCalvin
108aba0fe49SCalvin/*
109aba0fe49SCalvin * fd.io coding-style-patch-verification: ON
110aba0fe49SCalvin *
111aba0fe49SCalvin * Local Variables:
112aba0fe49SCalvin * eval: (c-set-style "gnu")
113aba0fe49SCalvin * End:
114aba0fe49SCalvin */
115