From 6767a2cce9a6412b3a41a927c4d56b9f0e1ec36f Mon Sep 17 00:00:00 2001 From: Justin Pettit Date: Wed, 2 Feb 2011 11:33:20 -0800 Subject: [PATCH] lib: Replace IP_TYPE_ references with IPPROTO_. A few common IP protocol types were defined in "lib/packets.h". However, we already assume the existence of which contains a more exhaustive list and should be available on POSIX systems. --- lib/classifier.c | 8 ++++---- lib/dhcp-client.c | 6 +++--- lib/dpif-netdev.c | 4 ++-- lib/flow.c | 10 +++++----- lib/nx-match.c | 6 +++--- lib/nx-match.def | 12 ++++++------ lib/odp-util.c | 18 +++++++++--------- lib/ofp-parse.c | 6 +++--- lib/ofp-print.c | 8 ++++---- lib/packets.h | 4 ---- ofproto/in-band.c | 10 +++++----- ofproto/netflow.c | 2 +- tests/flowgen.pl | 6 +++--- tests/test-classifier.c | 2 +- 14 files changed, 49 insertions(+), 53 deletions(-) diff --git a/lib/classifier.c b/lib/classifier.c index b05b491e..53280e50 100644 --- a/lib/classifier.c +++ b/lib/classifier.c @@ -368,11 +368,11 @@ cls_rule_format(const struct cls_rule *rule, struct ds *s) if (f->dl_type == htons(ETH_TYPE_IP)) { if (!(w & FWW_NW_PROTO)) { skip_proto = true; - if (f->nw_proto == IP_TYPE_ICMP) { + if (f->nw_proto == IPPROTO_ICMP) { ds_put_cstr(s, "icmp,"); - } else if (f->nw_proto == IP_TYPE_TCP) { + } else if (f->nw_proto == IPPROTO_TCP) { ds_put_cstr(s, "tcp,"); - } else if (f->nw_proto == IP_TYPE_UDP) { + } else if (f->nw_proto == IPPROTO_UDP) { ds_put_cstr(s, "udp,"); } else { ds_put_cstr(s, "ip,"); @@ -470,7 +470,7 @@ cls_rule_format(const struct cls_rule *rule, struct ds *s) if (!(w & FWW_NW_TOS)) { ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos); } - if (f->nw_proto == IP_TYPE_ICMP) { + if (f->nw_proto == IPPROTO_ICMP) { if (!(w & FWW_TP_SRC)) { ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src)); } diff --git a/lib/dhcp-client.c b/lib/dhcp-client.c index 74fa44c5..b9ebefe0 100644 --- a/lib/dhcp-client.c +++ b/lib/dhcp-client.c @@ -953,7 +953,7 @@ do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg) flow_extract(&b, 0, 0, &flow); if (flow.dl_type != htons(ETH_TYPE_IP) - || flow.nw_proto != IP_TYPE_UDP + || flow.nw_proto != IPPROTO_UDP || flow.tp_dst != htons(DHCP_CLIENT_PORT) || !(eth_addr_is_broadcast(flow.dl_dst) || eth_addr_equals(flow.dl_dst, cli_mac))) { @@ -1025,7 +1025,7 @@ do_send_msg(struct dhclient *cli, const struct dhcp_msg *msg) nh.ip_id = 0; nh.ip_frag_off = htons(IP_DONT_FRAGMENT); nh.ip_ttl = 64; - nh.ip_proto = IP_TYPE_UDP; + nh.ip_proto = IPPROTO_UDP; nh.ip_csum = 0; nh.ip_src = dhclient_get_ip(cli); /* XXX need to use UDP socket for nonzero server IPs so that we can get @@ -1046,7 +1046,7 @@ do_send_msg(struct dhclient *cli, const struct dhcp_msg *msg) th.udp_csum = 0; udp_csum = csum_add32(0, nh.ip_src); udp_csum = csum_add32(udp_csum, nh.ip_dst); - udp_csum = csum_add16(udp_csum, IP_TYPE_UDP << 8); + udp_csum = csum_add16(udp_csum, IPPROTO_UDP << 8); udp_csum = csum_add16(udp_csum, th.udp_len); udp_csum = csum_continue(udp_csum, &th, sizeof th); th.udp_csum = csum_finish(csum_continue(udp_csum, b.data, b.size)); diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 417ed6e6..acc14a8b 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -1216,10 +1216,10 @@ dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key, uint32_t *field; field = type == ODP_ACTION_ATTR_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst; - if (key->nw_proto == IP_TYPE_TCP && packet->l7) { + if (key->nw_proto == IPPROTO_TCP && packet->l7) { struct tcp_header *th = packet->l4; th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip); - } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) { + } else if (key->nw_proto == IPPROTO_UDP && packet->l7) { struct udp_header *uh = packet->l4; if (uh->udp_csum) { uh->udp_csum = recalc_csum32(uh->udp_csum, *field, ip); diff --git a/lib/flow.c b/lib/flow.c index 6842d3b5..7be10e29 100644 --- a/lib/flow.c +++ b/lib/flow.c @@ -183,21 +183,21 @@ flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t in_port, flow->nw_proto = nh->ip_proto; packet->l4 = b.data; if (!IP_IS_FRAGMENT(nh->ip_frag_off)) { - if (flow->nw_proto == IP_TYPE_TCP) { + if (flow->nw_proto == IPPROTO_TCP) { const struct tcp_header *tcp = pull_tcp(&b); if (tcp) { flow->tp_src = tcp->tcp_src; flow->tp_dst = tcp->tcp_dst; packet->l7 = b.data; } - } else if (flow->nw_proto == IP_TYPE_UDP) { + } else if (flow->nw_proto == IPPROTO_UDP) { const struct udp_header *udp = pull_udp(&b); if (udp) { flow->tp_src = udp->udp_src; flow->tp_dst = udp->udp_dst; packet->l7 = b.data; } - } else if (flow->nw_proto == IP_TYPE_ICMP) { + } else if (flow->nw_proto == IPPROTO_ICMP) { const struct icmp_header *icmp = pull_icmp(&b); if (icmp) { flow->icmp_type = htons(icmp->icmp_type); @@ -240,7 +240,7 @@ flow_extract_stats(const struct flow *flow, struct ofpbuf *packet, memset(stats, 0, sizeof(*stats)); if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) { - if ((flow->nw_proto == IP_TYPE_TCP) && packet->l7) { + if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) { struct tcp_header *tcp = packet->l4; stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl); } @@ -486,7 +486,7 @@ flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis) if (fields.eth_type == htons(ETH_TYPE_IP)) { fields.ip_addr = flow->nw_src ^ flow->nw_dst; fields.ip_proto = flow->nw_proto; - if (fields.ip_proto == IP_TYPE_TCP || fields.ip_proto == IP_TYPE_UDP) { + if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) { fields.tp_addr = flow->tp_src ^ flow->tp_dst; } else { fields.tp_addr = htons(0); diff --git a/lib/nx-match.c b/lib/nx-match.c index fcc1081e..7326b114 100644 --- a/lib/nx-match.c +++ b/lib/nx-match.c @@ -656,7 +656,7 @@ nx_put_match(struct ofpbuf *b, const struct cls_rule *cr) nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto); switch (flow->nw_proto) { /* TCP. */ - case IP_TYPE_TCP: + case IPPROTO_TCP: if (!(wc & FWW_TP_SRC)) { nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src); } @@ -666,7 +666,7 @@ nx_put_match(struct ofpbuf *b, const struct cls_rule *cr) break; /* UDP. */ - case IP_TYPE_UDP: + case IPPROTO_UDP: if (!(wc & FWW_TP_SRC)) { nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src); } @@ -676,7 +676,7 @@ nx_put_match(struct ofpbuf *b, const struct cls_rule *cr) break; /* ICMP. */ - case IP_TYPE_ICMP: + case IPPROTO_ICMP: if (!(wc & FWW_TP_SRC)) { nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src)); } diff --git a/lib/nx-match.def b/lib/nx-match.def index 09f375bd..d3e240ed 100644 --- a/lib/nx-match.def +++ b/lib/nx-match.def @@ -29,12 +29,12 @@ DEFINE_FIELD (OF_IP_TOS, FWW_NW_TOS, ETH_TYPE_IP, 0, false) DEFINE_FIELD (OF_IP_PROTO, FWW_NW_PROTO, ETH_TYPE_IP, 0, false) DEFINE_FIELD_M(OF_IP_SRC, 0, ETH_TYPE_IP, 0, false) DEFINE_FIELD_M(OF_IP_DST, 0, ETH_TYPE_IP, 0, false) -DEFINE_FIELD (OF_TCP_SRC, FWW_TP_SRC, ETH_TYPE_IP, IP_TYPE_TCP, false) -DEFINE_FIELD (OF_TCP_DST, FWW_TP_DST, ETH_TYPE_IP, IP_TYPE_TCP, false) -DEFINE_FIELD (OF_UDP_SRC, FWW_TP_SRC, ETH_TYPE_IP, IP_TYPE_UDP, false) -DEFINE_FIELD (OF_UDP_DST, FWW_TP_DST, ETH_TYPE_IP, IP_TYPE_UDP, false) -DEFINE_FIELD (OF_ICMP_TYPE, FWW_TP_SRC, ETH_TYPE_IP, IP_TYPE_ICMP, false) -DEFINE_FIELD (OF_ICMP_CODE, FWW_TP_DST, ETH_TYPE_IP, IP_TYPE_ICMP, false) +DEFINE_FIELD (OF_TCP_SRC, FWW_TP_SRC, ETH_TYPE_IP, IPPROTO_TCP, false) +DEFINE_FIELD (OF_TCP_DST, FWW_TP_DST, ETH_TYPE_IP, IPPROTO_TCP, false) +DEFINE_FIELD (OF_UDP_SRC, FWW_TP_SRC, ETH_TYPE_IP, IPPROTO_UDP, false) +DEFINE_FIELD (OF_UDP_DST, FWW_TP_DST, ETH_TYPE_IP, IPPROTO_UDP, false) +DEFINE_FIELD (OF_ICMP_TYPE, FWW_TP_SRC, ETH_TYPE_IP, IPPROTO_ICMP, false) +DEFINE_FIELD (OF_ICMP_CODE, FWW_TP_DST, ETH_TYPE_IP, IPPROTO_ICMP, false) DEFINE_FIELD (OF_ARP_OP, FWW_NW_PROTO, ETH_TYPE_ARP, 0, false) DEFINE_FIELD_M(OF_ARP_SPA, 0, ETH_TYPE_ARP, 0, false) DEFINE_FIELD_M(OF_ARP_TPA, 0, ETH_TYPE_ARP, 0, false) diff --git a/lib/odp-util.c b/lib/odp-util.c index e50b5785..5f1a77ce 100644 --- a/lib/odp-util.c +++ b/lib/odp-util.c @@ -386,21 +386,21 @@ odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow) ipv4_key->ipv4_proto = flow->nw_proto; ipv4_key->ipv4_tos = flow->nw_tos; - if (flow->nw_proto == IP_TYPE_TCP) { + if (flow->nw_proto == IPPROTO_TCP) { struct odp_key_tcp *tcp_key; tcp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_TCP, sizeof *tcp_key); tcp_key->tcp_src = flow->tp_src; tcp_key->tcp_dst = flow->tp_dst; - } else if (flow->nw_proto == IP_TYPE_UDP) { + } else if (flow->nw_proto == IPPROTO_UDP) { struct odp_key_udp *udp_key; udp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_UDP, sizeof *udp_key); udp_key->udp_src = flow->tp_src; udp_key->udp_dst = flow->tp_dst; - } else if (flow->nw_proto == IP_TYPE_ICMP) { + } else if (flow->nw_proto == IPPROTO_ICMP) { struct odp_key_icmp *icmp_key; icmp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ICMP, @@ -504,7 +504,7 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, break; case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP): - if (flow->nw_proto != IP_TYPE_TCP) { + if (flow->nw_proto != IPPROTO_TCP) { return EINVAL; } tcp_key = nl_attr_get(nla); @@ -513,7 +513,7 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, break; case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP): - if (flow->nw_proto != IP_TYPE_UDP) { + if (flow->nw_proto != IPPROTO_UDP) { return EINVAL; } udp_key = nl_attr_get(nla); @@ -522,7 +522,7 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, break; case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP): - if (flow->nw_proto != IP_TYPE_ICMP) { + if (flow->nw_proto != IPPROTO_ICMP) { return EINVAL; } icmp_key = nl_attr_get(nla); @@ -577,9 +577,9 @@ odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, return 0; case ODP_KEY_ATTR_IPV4: - if (flow->nw_proto == IP_TYPE_TCP - || flow->nw_proto == IP_TYPE_UDP - || flow->nw_proto == IP_TYPE_ICMP) { + if (flow->nw_proto == IPPROTO_TCP + || flow->nw_proto == IPPROTO_UDP + || flow->nw_proto == IPPROTO_ICMP) { return EINVAL; } return 0; diff --git a/lib/ofp-parse.c b/lib/ofp-parse.c index 7fed553e..65d7a654 100644 --- a/lib/ofp-parse.c +++ b/lib/ofp-parse.c @@ -460,9 +460,9 @@ parse_protocol(const char *name, const struct protocol **p_out) static const struct protocol protocols[] = { { "ip", ETH_TYPE_IP, 0 }, { "arp", ETH_TYPE_ARP, 0 }, - { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP }, - { "tcp", ETH_TYPE_IP, IP_TYPE_TCP }, - { "udp", ETH_TYPE_IP, IP_TYPE_UDP }, + { "icmp", ETH_TYPE_IP, IPPROTO_ICMP }, + { "tcp", ETH_TYPE_IP, IPPROTO_TCP }, + { "udp", ETH_TYPE_IP, IPPROTO_UDP }, }; const struct protocol *p; diff --git a/lib/ofp-print.c b/lib/ofp-print.c index 5f43b81e..73820e8b 100644 --- a/lib/ofp-print.c +++ b/lib/ofp-print.c @@ -749,11 +749,11 @@ ofp_match_to_string(const struct ofp_match *om, int verbosity) if (om->dl_type == htons(ETH_TYPE_IP)) { if (!(w & OFPFW_NW_PROTO)) { skip_proto = true; - if (om->nw_proto == IP_TYPE_ICMP) { + if (om->nw_proto == IPPROTO_ICMP) { ds_put_cstr(&f, "icmp,"); - } else if (om->nw_proto == IP_TYPE_TCP) { + } else if (om->nw_proto == IPPROTO_TCP) { ds_put_cstr(&f, "tcp,"); - } else if (om->nw_proto == IP_TYPE_UDP) { + } else if (om->nw_proto == IPPROTO_UDP) { ds_put_cstr(&f, "udp,"); } else { ds_put_cstr(&f, "ip,"); @@ -800,7 +800,7 @@ ofp_match_to_string(const struct ofp_match *om, int verbosity) } print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity, "%u", om->nw_tos); - if (om->nw_proto == IP_TYPE_ICMP) { + if (om->nw_proto == IPPROTO_ICMP) { print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity, "%d", ntohs(om->icmp_type)); print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity, diff --git a/lib/packets.h b/lib/packets.h index 96e23e18..c0480db7 100644 --- a/lib/packets.h +++ b/lib/packets.h @@ -286,10 +286,6 @@ ip_is_cidr(ovs_be32 netmask) #define IP_ECN_MASK 0x03 #define IP_DSCP_MASK 0xfc -#define IP_TYPE_ICMP 1 -#define IP_TYPE_TCP 6 -#define IP_TYPE_UDP 17 - #define IP_VERSION 4 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */ diff --git a/ofproto/in-band.c b/ofproto/in-band.c index 94d2cb57..be4a7e89 100644 --- a/ofproto/in-band.c +++ b/ofproto/in-band.c @@ -403,7 +403,7 @@ in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow, /* Regardless of how the flow table is configured, we want to be * able to see replies to our DHCP requests. */ if (flow->dl_type == htons(ETH_TYPE_IP) - && flow->nw_proto == IP_TYPE_UDP + && flow->nw_proto == IPPROTO_UDP && flow->tp_src == htons(DHCP_SERVER_PORT) && flow->tp_dst == htons(DHCP_CLIENT_PORT) && packet->l7) { @@ -438,7 +438,7 @@ in_band_rule_check(struct in_band *in_band, const struct flow *flow, /* Don't allow flows that would prevent DHCP replies from being seen * by the local port. */ if (flow->dl_type == htons(ETH_TYPE_IP) - && flow->nw_proto == IP_TYPE_UDP + && flow->nw_proto == IPPROTO_UDP && flow->tp_src == htons(DHCP_SERVER_PORT) && flow->tp_dst == htons(DHCP_CLIENT_PORT)) { const struct nlattr *a; @@ -469,7 +469,7 @@ make_rules(struct in_band *ib, cls_rule_set_in_port(&rule, ODPP_LOCAL); cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP)); cls_rule_set_dl_src(&rule, ib->installed_local_mac); - cls_rule_set_nw_proto(&rule, IP_TYPE_UDP); + cls_rule_set_nw_proto(&rule, IPPROTO_UDP); cls_rule_set_tp_src(&rule, htons(DHCP_CLIENT_PORT)); cls_rule_set_tp_dst(&rule, htons(DHCP_SERVER_PORT)); cb(ib, &rule); @@ -542,7 +542,7 @@ make_rules(struct in_band *ib, /* (h) Allow TCP traffic to the remote's IP and port. */ cls_rule_init_catchall(&rule, IBR_TO_REMOTE_TCP); cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP)); - cls_rule_set_nw_proto(&rule, IP_TYPE_TCP); + cls_rule_set_nw_proto(&rule, IPPROTO_TCP); cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr); cls_rule_set_tp_dst(&rule, a->sin_port); cb(ib, &rule); @@ -550,7 +550,7 @@ make_rules(struct in_band *ib, /* (i) Allow TCP traffic from the remote's IP and port. */ cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_TCP); cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP)); - cls_rule_set_nw_proto(&rule, IP_TYPE_TCP); + cls_rule_set_nw_proto(&rule, IPPROTO_TCP); cls_rule_set_nw_src(&rule, a->sin_addr.s_addr); cls_rule_set_tp_src(&rule, a->sin_port); cb(ib, &rule); diff --git a/ofproto/netflow.c b/ofproto/netflow.c index 37b18ffd..77572fe5 100644 --- a/ofproto/netflow.c +++ b/ofproto/netflow.c @@ -148,7 +148,7 @@ gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow, nf_rec->init_time = htonl(nf_flow->created - nf->boot_time); nf_rec->used_time = htonl(MAX(nf_flow->created, expired->used) - nf->boot_time); - if (expired->flow.nw_proto == IP_TYPE_ICMP) { + if (expired->flow.nw_proto == IPPROTO_ICMP) { /* In NetFlow, the ICMP type and code are concatenated and * placed in the 'dst_port' field. */ uint8_t type = ntohs(expired->flow.tp_src); diff --git a/tests/flowgen.pl b/tests/flowgen.pl index a14faa70..4e2e5ad7 100755 --- a/tests/flowgen.pl +++ b/tests/flowgen.pl @@ -84,15 +84,15 @@ sub output { $flow{NW_PROTO} = 42; } elsif ($attrs{TP_PROTO} eq 'TCP' || $attrs{TP_PROTO} eq 'TCP+options') { - $flow{NW_PROTO} = 6; # IP_TYPE_TCP + $flow{NW_PROTO} = 6; # IPPROTO_TCP $flow{TP_SRC} = 6667; $flow{TP_DST} = 9998; } elsif ($attrs{TP_PROTO} eq 'UDP') { - $flow{NW_PROTO} = 17; # IP_TYPE_UDP + $flow{NW_PROTO} = 17; # IPPROTO_UDP $flow{TP_SRC} = 1112; $flow{TP_DST} = 2223; } elsif ($attrs{TP_PROTO} eq 'ICMP') { - $flow{NW_PROTO} = 1; # IP_TYPE_ICMP + $flow{NW_PROTO} = 1; # IPPROTO_ICMP $flow{TP_SRC} = 8; # echo request $flow{TP_DST} = 0; # code } else { diff --git a/tests/test-classifier.c b/tests/test-classifier.c index e1f93a09..9af8aacb 100644 --- a/tests/test-classifier.c +++ b/tests/test-classifier.c @@ -262,7 +262,7 @@ static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 }, { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } }; static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; -static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP }; +static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP }; static uint8_t nw_tos_values[] = { 49, 0 }; static void *values[CLS_N_FIELDS][2]; -- 2.30.2