Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #ifndef vnsw_agent_proto_handler_hpp
6 : #define vnsw_agent_proto_handler_hpp
7 :
8 : #include "pkt_handler.h"
9 :
10 : // Pseudo header for UDP checksum
11 : struct PseudoUdpHdr {
12 : in_addr_t src;
13 : in_addr_t dest;
14 : uint8_t res;
15 : uint8_t prot;
16 : uint16_t len;
17 0 : PseudoUdpHdr(in_addr_t s, in_addr_t d, uint8_t p, uint16_t l) :
18 0 : src(s), dest(d), res(0), prot(p), len(l) { }
19 : };
20 :
21 : struct vlanhdr {
22 : uint16_t tpid;
23 : uint16_t tci;
24 : };
25 :
26 : // Protocol handler, to process an incoming packet
27 : // Each protocol has a handler derived from this
28 : class ProtoHandler {
29 : public:
30 : ProtoHandler(Agent *agent, boost::shared_ptr<PktInfo> info,
31 : boost::asio::io_context &io);
32 : virtual ~ProtoHandler();
33 :
34 : virtual bool Run() = 0;
35 :
36 : void Send(uint32_t itf, uint32_t vrf, uint16_t, PktHandler::PktModuleName);
37 : void Send(uint32_t itf, uint32_t vrf, uint16_t cmd,
38 : uint32_t param1, uint32_t param2, PktHandler::PktModuleName mod);
39 :
40 : int EthHdr(const MacAddress &src, const MacAddress &dest,
41 : const uint16_t proto);
42 : int EthHdr(char *buff, uint16_t len, const MacAddress &src,
43 : const MacAddress &dest, const uint16_t proto, uint16_t vlan_id);
44 : int EthHdr(char *buff, uint16_t len, const Interface *interface,
45 : const MacAddress &src, const MacAddress &dest,
46 : const uint16_t proto);
47 : int EthHdr(char *buff, uint16_t len, uint32_t ifindex,
48 : const MacAddress &src, const MacAddress &dest,
49 : const uint16_t proto);
50 :
51 : void VlanHdr(uint8_t *ptr, uint16_t tci);
52 : void IpHdr(uint16_t len, in_addr_t src, in_addr_t dest, uint8_t protocol,
53 : uint16_t id, uint8_t ttl);
54 : uint16_t IpHdr(char *buff, uint16_t buf_len, uint16_t len, in_addr_t src,
55 : in_addr_t dest, uint8_t protocol, uint16_t id, uint8_t ttl);
56 : void Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header,
57 : uint8_t hlim, uint8_t *src, uint8_t *dest);
58 : void UdpHdr(uint16_t len, in_addr_t src, uint16_t src_port, in_addr_t dest,
59 : uint16_t dest_port);
60 : void UdpHdr(udphdr *hdr, uint16_t len, const uint8_t *src, uint16_t src_port,
61 : const uint8_t *dest, uint16_t dest_port, uint8_t next_hdr);
62 : void UdpHdr(uint16_t len, const uint8_t *src, uint16_t src_port,
63 : const uint8_t *dest, uint16_t dest_port, uint8_t next_hdr);
64 : uint16_t UdpHdr(udphdr *udp, uint16_t buf_len, uint16_t len, in_addr_t src,
65 : uint16_t src_port, in_addr_t dest, uint16_t dest_port);
66 : uint16_t IcmpHdr(char *buff, uint16_t buf_len, uint8_t type, uint8_t code,
67 : uint16_t word1, uint16_t word2);
68 : void IcmpChecksum(char *buff, uint16_t buf_len);
69 :
70 : void IgmpChecksum(char *buff, uint16_t buf_len);
71 :
72 : uint32_t Sum(uint16_t *, std::size_t, uint32_t) const;
73 : uint16_t Csum(uint16_t *, std::size_t, uint32_t) const;
74 : uint16_t UdpCsum(in_addr_t, in_addr_t, std::size_t, udphdr *) const;
75 : uint16_t Ipv6Csum(const uint8_t *src, const uint8_t *dest,
76 : uint16_t plen, uint8_t next_hdr, uint16_t *hdr) const;
77 : uint16_t Icmpv6Csum(const uint8_t *src, const uint8_t *dest,
78 : icmp6_hdr *icmp, uint16_t plen) const;
79 :
80 710 : Agent *agent() const { return agent_; }
81 0 : uint32_t GetVrfIndex() const { return pkt_info_->GetAgentHdr().vrf; }
82 0 : uint32_t GetInterfaceIndex() const {
83 0 : return pkt_info_->GetAgentHdr().ifindex;
84 : }
85 : uint16_t GetLength() const { return pkt_info_->len; }
86 : uint32_t GetCmdParam() const { return pkt_info_->GetAgentHdr().cmd_param; }
87 :
88 0 : PktInfo *pkt_info() const { return pkt_info_.get(); }
89 : uint32_t EncapHeaderLen() const;
90 : protected:
91 : Agent *agent_;
92 : boost::shared_ptr<PktInfo> pkt_info_;
93 : boost::asio::io_context &io_;
94 :
95 : private:
96 : void FillUdpHdr(udphdr *udp, uint16_t len,
97 : uint16_t src_port, uint16_t dest_port);
98 :
99 : DISALLOW_COPY_AND_ASSIGN(ProtoHandler);
100 : };
101 :
102 : #endif // vnsw_agent_proto_handler_hpp
|