Beispiel #1
0
func (self *OxmValueMask) Merge(vm OxmValueMask) error {
	mask := bytes2.And(self.Mask, vm.Mask)
	if bytes.Equal(bytes2.And(self.Value, mask), bytes2.And(vm.Value, mask)) {
		value := func(o OxmValueMask) []byte {
			if len(o.Mask) > 0 {
				return bytes2.And(o.Value, o.Mask)
			} else {
				return o.Value
			}
		}
		self.Value = bytes2.Or(value(*self), value(vm))
		self.Mask = bytes2.Or(self.Mask, vm.Mask)
		return nil
	} else {
		return fmt.Errorf("conflict")
	}
}
Beispiel #2
0
func (self StratosOxm) SetField(data *ofp4sw.Frame, key ofp4sw.OxmKey, payload ofp4sw.OxmPayload) error {
	switch k := key.(type) {
	case OxmKeyStratos:
		switch k.Field {
		case oxm.STRATOS_OXM_FIELD_BASIC:
			p := payload.(ofp4sw.OxmValueMask)

			fetch11 := func() *layers.Dot11 {
				for _, layer := range data.Layers() {
					switch l := layer.(type) {
					case *layers.Dot11:
						return l
					}
				}
				return nil
			}

			fetchIeList := func() (ret []*layers.Dot11InformationElement) {
				for _, layer := range data.Layers() {
					switch l := layer.(type) {
					case *layers.Dot11InformationElement:
						ret = append(ret, l)
					}
				}
				return
			}

			switch k.Type {
			case oxm.STROXM_BASIC_DOT11:
				data.Oob[key] = ofp4sw.OxmValueMask{
					Value: p.Value,
				}
			case oxm.STROXM_BASIC_DOT11_FRAME_CTRL:
				if m := fetch11(); m != nil {
					v := []byte{
						uint8(m.Type<<2) | m.Proto,
						uint8(m.Flags),
					}
					v = bytes2.Or(p.Value, bytes2.And(v, p.Mask))
					m.Proto = v[0] & 0x03
					m.Type = layers.Dot11Type(v[0] >> 2)
					m.Flags = layers.Dot11Flags(v[1])
				}
			case oxm.STROXM_BASIC_DOT11_ADDR1:
				if m := fetch11(); m != nil {
					if len(p.Mask) > 0 {
						m.Address1 = bytes2.Or(p.Value, bytes2.And([]byte(m.Address1), p.Mask))
					} else {
						m.Address1 = p.Value
					}
				}
			case oxm.STROXM_BASIC_DOT11_ADDR2:
				if m := fetch11(); m != nil {
					if len(p.Mask) > 0 {
						m.Address2 = bytes2.Or(p.Value, bytes2.And([]byte(m.Address2), p.Mask))
					} else {
						m.Address2 = p.Value
					}
				}
			case oxm.STROXM_BASIC_DOT11_ADDR3:
				if m := fetch11(); m != nil {
					if len(p.Mask) > 0 {
						m.Address3 = bytes2.Or(p.Value, bytes2.And([]byte(m.Address3), p.Mask))
					} else {
						m.Address3 = p.Value
					}
				}
			case oxm.STROXM_BASIC_DOT11_ADDR4:
				if m := fetch11(); m != nil {
					if len(p.Mask) > 0 {
						m.Address4 = bytes2.Or(p.Value, bytes2.And([]byte(m.Address4), p.Mask))
					} else {
						m.Address4 = p.Value
					}
				}
			case oxm.STROXM_BASIC_DOT11_SSID:
				for _, l := range fetchIeList() {
					if l.ID == 0 {
						if len(p.Mask) > 0 {
							l.Info = bytes2.Or(p.Value, bytes2.And(l.Info, p.Mask))
						} else {
							l.Info = p.Value
						}
					}
				}
			default:
				return fmt.Errorf("unsupported oxm experimenter type")
			}
		case oxm.STRATOS_OXM_FIELD_RADIOTAP:
			p := payload.(ofp4sw.OxmValueMask)

			if v, ok := data.Oob[key].(ofp4sw.OxmValueMask); !ok {
				data.Oob[key] = payload
			} else if len(p.Mask) > 0 {
				v.Value = bytes2.Or(p.Value, bytes2.And(v.Value, p.Mask))
				data.Oob[key] = v
			} else {
				data.Oob[key] = ofp4sw.OxmValueMask{
					Value: p.Value,
				}
			}
		default:
			return fmt.Errorf("unsupported oxm experimenter field")
		}
	default:
		return fmt.Errorf("unknown oxm key")
	}
	return nil
}