summaryrefslogtreecommitdiff
path: root/trafgen_proto.c
AgeCommit message (Collapse)AuthorFilesLines
2017-06-02trafgen: proto: Allow to set field with variable lengthVadim Kochan1-26/+76
It is quite tricky to set field value with a variable length (i.e. DNS query name), to make it possible the field needs to be added to header with 'len=0' in that case there will be no any payload allocation, but only while setting the field value the packet will be appended with a real length bytes and after the field needs to be relocated to the right place. Also add 'len' parameter to *_set_bytes(...) functoins to have better control over it. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2017-05-15trafgen: parser: Use proto_field_set_xxx where it is possibleVadim Kochan1-0/+5
Use proto_field_set_xxx(field, ...) instead of proto_hdr_field_set_xxx(hdr, fid, ...) to be more generic and do not depend on 'hdr' variable. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2017-02-06trafgen: l3: Make possible to send frames via tun deviceVadim Kochan1-0/+5
tun interface does not have Ethernet header so lets push Ethernet header only if device supports this. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-12-21trafgen: parser: Allow to set function at field offsetVadim Kochan1-16/+11
Extend proto field expression to: proto_field[{index}:{len}] = {func} which allows to specify function on the field offset via index and value length (default is 1 - 1 byte). This rule is optional. It was needed to keep of proto_field's copies in packet_dyn->fields instead of original fields which allows to scpecify different functions on the different parts of same field, also the copy of original proto_field allows to set custom length/pkt_offset which makes such field behave as virtual sub-field of the original one with different length/pkt_offset but point to the same piece of header. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-12-21trafgen: proto: Add proto field only setters/gettersVadim Kochan1-0/+46
Add proto_field_xxx functions to set/get value via specified proto_field only. It is good to have such API in case if application needs to set/get value for some custom proto_field instance. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-12-21trafgen: proto: Rename proto_field_xxx -> proto_hdr_field_xxxVadim Kochan1-71/+73
Rename all proto_field_xxx(...) functions to proto_hdr_field(...). It is good for 2 reasons: 1) proto_hdr_field_xxx naming is more consistent as it is related to proto_hdr API. 2) It makes possible to introduce proto_field_xxx API which will operate only with struct proto_field. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-12-21trafgen: proto: Change __proto_field_set_bytes(...) to take struct proto_fieldVadim Kochan1-17/+33
Change __proto_field_set_bytes(...) function to take struct proto_field instead of doing lookup by hdr & fid. It is needed to able use this function with some custom modified struct proto_field (len, pkt_offset). Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-12-05trafgen: l3: Support interface without IP addressKen Wu1-4/+8
Move default source address setting to packet_finish so that we do not need to get the device's address if the source address is set in the packet. Without this, trafgen does not work with an interface without address configured. In addition, in the case failing to get the address for an interface, intead of panic, it now prints a warning and uses a value of 0.0.0.0. Signed-off-by: Ken Wu <abawwu@gmail.com> Reviewed-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-10-21trafgen: proto: Don't drop const qualifier during castTobias Klauser1-2/+2
Don't drop const qualifier during cast to uint32_t * Found by compiling with -Wcast-qual. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-09-21trafgen: parser: Unify proto field value parsingVadim Kochan1-1/+1
Changed parsing logic of field value expression to be more generic. Such approach will allow to easy extend field value expression to support dynamic field functions. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-09-21trafgen: proto: Introduce proto_upper_header() functionVadim Kochan1-0/+12
Added proto_upper_header() function to fetch upper proto header. It will be used by IPv4/IPv6 proto header to notify UDP/TCP proto headers to invalidate L4 csum when L3 pseudo header field was changed. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-09-21trafgen: proto: Improve to find lower header by indexVadim Kochan1-14/+11
Extended struct proto_hdr with 'index' field which is used for faster lookup of lower header without doing a loop. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-09-21trafgen: proto: Randomize proto field at runtimeVadim Kochan1-0/+27
Add dynamic proto field function which can generate random value in specified range (default 0 - MAX_UINT32). Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-09-21trafgen: proto: Increment proto field at runtimeVadim Kochan1-0/+54
Implement incrementing of proto field at runtime with min & max parameters, by default if the 'min' parameter is not specified then original value is used. For fields which len is greater than 4 - last 4 bytes are incremented as 4 byte value (this trick is used to increment MAC/IPv6 addresses). Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-09-21trafgen: proto: Update field value at runtimeVadim Kochan1-0/+32
Add basic infrastructure for update proto field value at runtime by dynamic field function. Extended 'struct packet_dyn' with proto fields which has dynamically changing values at runtime. Added 'field_changed' callback for proto header which may be used for check if csum updating is needed. This callback is called after field was changed at runtime. Added 'packet_update' callback to let proto header know when to apply final proto header changes at runtime (e.g. - csum update). Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-12trafgen: proto: Split static protocol definition out of struct proto_hdrTobias Klauser1-31/+34
Currently struct proto_hdr is used twofold: 1) Statically define protocol behavior, i.e. all the *_hdr definitions in trafgen_l{2,3,4}.c which map a protocol id/layer to a set of callback functions. 2) For each packet created at parse time the struct is memcpy()'ed (including all the static information from 1) and then used to store dynamic information at parse/run time. Thus, struct proto_hdr members such as the proto id, layer and the pointers callback functions get copied for each created packet (in addition to the other fields which get changed during parsing). Also, static/dynamic information get mixed and we e.g. can't make the protocol definitions const to ensure they'll not get changed by mistake. Rather than copying the struct proto_hdr for every packet, clearly separate the two purposes defined above by splitting struct proto_hdr into two structs: 1) struct proto_ops for the static (const) protocol behavior definition 2) struct proto_hdr (reduced) for dynamic information struct proto_hdr keeps a pointer to the corresponding proto_ops instance and uses it to execute the corresponding callbacks. Reference: https://groups.google.com/forum/#!msg/netsniff-ng/20RvwJdh50Y/eMkbmKSaBgAJ Acked-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-09trafgen: proto: Make bytes pointer const in proto field set functionsTobias Klauser1-3/+6
Make the __proto_field_set_bytes() take a const uint8_t *bytes pointer and also changed all callers accordingly. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-09trafgen: proto: Store registered protocols in an arrayTobias Klauser1-12/+7
Protocols are registered early at startup and aren't changed at runtime. In order to speed up lookup while parsing, store the pointers to the protocol definitions (struct proto_hdr) in an array, indexed by protocol id rather than in a linked list. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-09trafgen: proto: Don't store context in each proto headerTobias Klauser1-12/+7
Use a module variable to access the proto context instead of storing a pointer in every header. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-05trafgen: proto: Use field id as array indexVadim Kochan1-6/+5
Usually proto fields array are sorted in the same order as the respective enum. Thus, the id can be used used as an array index for faster lookup. Add an explanatory comment and enforce the correspondence of id and index using bug_on(). This will make csum field calculation a little faster at runtime. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-02trafgen: proto: Move proto headers into packetVadim Kochan1-21/+9
Until now headers were used only for packet creation at compile time, which does not allow to handle dynamic field updates at runtime. To support dynamic updates, it is necessary to keep the proto_hdr entries around after packet compilation so we can reference the header fields to dynamically update. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> [tk: Adjust patch description, simplify code] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-08-02trafgen: proto: Reference to packet from struct proto_hdrVadim Kochan1-6/+7
Using of current_packet() is not possible for dynamically updated fields so we need to keep the packet index in proto_hdr struct to reference the correct packet. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-07-18trafgen: ipv4: Do not use user-provided 'ihl' field to calculate csumVadim Kochan1-1/+3
It is potentially dangerous to use the user specified IHL field for csum calculation, as it might lead to read buffer overflows. Instead introduce and use the len field in struct proto_hdr which is calculated automatically after the header is built. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> [tk: reword commit message] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-04-25trafgen: proto: Add IPv6 header generationTobias Klauser1-4/+29
Support for generating simple IPv6 headers using the 'ip6()/ipv6()' trafgen generation function. Fields supported: ver|version Version (default: 6) tc|tclass Traffic class (default: 0) fl|flow Flow Label (default: 0) len|length Payload length (calculated by default) nh|nexthdr Type of next header (default: 0) hl|hoplimit|ttl Hop Limit, TTL (default: 0) sa|saddr Source IPv6 address (default: device address) da|daddr Destination IPv6 address (default: 0:0:0:0:0:0:0:0) Examples: { eth(), ipv6(daddr=1:2:3:4:5:6:7:8) } { ipv6(tc=2, hl=3, daddr=::1) } { eth(), ipv6(nh=58, sa=2001:db8::, da=::1), 128, 0, 0x52, 0x03, 0, 0, 0, 0 } If not explicitely specified, the lower header is initialized as Ethernet. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-04-22trafgen: proto: Replace panic() with bug() where applicableTobias Klauser1-2/+1
In some cases we use panic() in situations where the error could only happen due to an implementation error. Use bug() there as it is more applicable, as panic() is mainly used to report system call errors. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-04-21trafgen: proto: Inline proto_current_header() into its only callerTobias Klauser1-9/+1
proto_lower_header() is the only caller for proto_current_header() which already checks header_count > 0, so inline proto_current_header() and omit the additional check and a panic() which thus can never be triggered. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-02-23trafgen: proto: Fix bad field maskingVadim Kochan1-4/+20
Current logic does OR of existing field value & shift-masked specified value, which is not enough as 0s bits from the specified value will be not set (be cause of OR), so fixed it by reseting original field value by AND with reverted field mask, in otherwords - bits part of original field value is reset to 0s and only after OR-ed with specified shift-masked value. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-02-03trafgen: proto: Make proto_current_header() staticTobias Klauser1-1/+1
After commit 88b359d2aa7e ("trafgen: proto: Simplify getting lower protocol after init") proto_current_header() is no longer used outside trafgen_proto.c, so make it static. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-02-02trafgen: proto: Add set_next_proto callback to struct proto_hdrVadim Kochan1-5/+14
Add set_next_proto callback to proto_hdr struct to allow lower protocol set next protocol id by enum proto_id. Extended proto_lower_default_add(...) function to take upper protocol to delegate it's id to lower protocol to set next protocol field. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-02-02trafgen: proto: Simplify getting lower protocol after initVadim Kochan1-7/+10
Change proto_header_init(...) and proto_lower_default_add(...) functions to return struct proto_hdr * to do not call proto_current_header(...) after, so it makes more sense to get struct proto_hdr * right after initializing protocol by id. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-02-01trafgen: proto: Mark fields parameter to proto_header_fields_add as constTobias Klauser1-2/+2
It's not changed inside the function, so mark it as const. Also adjust the header guards. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-01-29trafgen: l4: Add UDP header generation logicVadim Kochan1-0/+2
Add trafgen_l4.c module with generation UDP header fields. UDP protocol generation logic automaticaly sets the protocol field of the lower level protocol to IPPROTO_UDP by default (if it is IPv4). Also checksum & length are calculated and set if it is not given by the user. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-01-29trafgen: l3: Add IPv4 header generation backendVadim Kochan1-0/+2
Add L3 module for implement L3 layer protocols generation. Implemented setting of IPv4 header with all fields except options. By default IPv4 address of output device is used as src ip address. On finish (after packet is specified) - total len & checksum are calculated. Meanwhile Ethernet protocol is initialized as default lower header. If the lower protocol is IPv4 then IPv4 protocol id is set to IP-in-IP in lower protocol header. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-01-28trafgen: proto: Add functon to fill field with device ipv4 addressVadim Kochan1-0/+32
Add helper which fills a device's ipv4 addr to the specified protocol field. It will be used by protocols like ARP, IPv4, etc. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-01-28trafgen: l2: Add Ethernet protocol header generationVadim Kochan1-1/+4
Add trafgen_l2.c module for generating L2 related headers. Add Ethernet header generating. By default source MAC address is used from the specified output device. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-01-28trafgen: proto: Add function to set field from device MACVadim Kochan1-0/+31
Add helper function to set device's MAC address to protocol field which may be used by Ethernet & ARP protocol header generation functions. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2016-01-28trafgen: Add basic protocol generation logicVadim Kochan1-0/+326
Add new trafgen_proto.c module with basic protocol header fields generation logic. This will allow to support protocol specific keywords in the trafgen configuration language. Each protocol must implement struct proto_hdr and register it to the global proto list. Protocol headers consist of a set of fields, and each field must be described via struct proto_field by specifying unique id, length and offset (relative to the header start). Fields smaller than 8 bits can be described via left shift & mask. The following callbacks are invoked to perform special actions to build the header during parsing: 1) header_init - required fields must be added to the packet and initialized with default values. 2) header_finish - it is invoked when header is specified, all user specified fields are set. 3) packet_finish - callback is invoked from upper to lower header to calculate fields depending on upper layers such as total length or checksum. The protocol generation API provides convenience protocol field setters/getters to to be used in the parser while crafting the packet. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> [tk: wordsmithing on commit message, minor variable type changes] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>