Back to home page

OSCL-LXR

 
 

    


0001 #include <stddef.h>
0002 #include <inttypes.h>
0003 #include <errno.h>
0004 #include <linux/seg6_local.h>
0005 #include <linux/bpf.h>
0006 #include <bpf/bpf_helpers.h>
0007 #include <bpf/bpf_endian.h>
0008 
0009 /* Packet parsing state machine helpers. */
0010 #define cursor_advance(_cursor, _len) \
0011     ({ void *_tmp = _cursor; _cursor += _len; _tmp; })
0012 
0013 #define SR6_FLAG_ALERT (1 << 4)
0014 
0015 #define BPF_PACKET_HEADER __attribute__((packed))
0016 
0017 struct ip6_t {
0018     unsigned int ver:4;
0019     unsigned int priority:8;
0020     unsigned int flow_label:20;
0021     unsigned short payload_len;
0022     unsigned char next_header;
0023     unsigned char hop_limit;
0024     unsigned long long src_hi;
0025     unsigned long long src_lo;
0026     unsigned long long dst_hi;
0027     unsigned long long dst_lo;
0028 } BPF_PACKET_HEADER;
0029 
0030 struct ip6_addr_t {
0031     unsigned long long hi;
0032     unsigned long long lo;
0033 } BPF_PACKET_HEADER;
0034 
0035 struct ip6_srh_t {
0036     unsigned char nexthdr;
0037     unsigned char hdrlen;
0038     unsigned char type;
0039     unsigned char segments_left;
0040     unsigned char first_segment;
0041     unsigned char flags;
0042     unsigned short tag;
0043 
0044     struct ip6_addr_t segments[0];
0045 } BPF_PACKET_HEADER;
0046 
0047 struct sr6_tlv_t {
0048     unsigned char type;
0049     unsigned char len;
0050     unsigned char value[0];
0051 } BPF_PACKET_HEADER;
0052 
0053 static __always_inline struct ip6_srh_t *get_srh(struct __sk_buff *skb)
0054 {
0055     void *cursor, *data_end;
0056     struct ip6_srh_t *srh;
0057     struct ip6_t *ip;
0058     uint8_t *ipver;
0059 
0060     data_end = (void *)(long)skb->data_end;
0061     cursor = (void *)(long)skb->data;
0062     ipver = (uint8_t *)cursor;
0063 
0064     if ((void *)ipver + sizeof(*ipver) > data_end)
0065         return NULL;
0066 
0067     if ((*ipver >> 4) != 6)
0068         return NULL;
0069 
0070     ip = cursor_advance(cursor, sizeof(*ip));
0071     if ((void *)ip + sizeof(*ip) > data_end)
0072         return NULL;
0073 
0074     if (ip->next_header != 43)
0075         return NULL;
0076 
0077     srh = cursor_advance(cursor, sizeof(*srh));
0078     if ((void *)srh + sizeof(*srh) > data_end)
0079         return NULL;
0080 
0081     if (srh->type != 4)
0082         return NULL;
0083 
0084     return srh;
0085 }
0086 
0087 static __always_inline
0088 int update_tlv_pad(struct __sk_buff *skb, uint32_t new_pad,
0089            uint32_t old_pad, uint32_t pad_off)
0090 {
0091     int err;
0092 
0093     if (new_pad != old_pad) {
0094         err = bpf_lwt_seg6_adjust_srh(skb, pad_off,
0095                       (int) new_pad - (int) old_pad);
0096         if (err)
0097             return err;
0098     }
0099 
0100     if (new_pad > 0) {
0101         char pad_tlv_buf[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0102                     0, 0, 0};
0103         struct sr6_tlv_t *pad_tlv = (struct sr6_tlv_t *) pad_tlv_buf;
0104 
0105         pad_tlv->type = SR6_TLV_PADDING;
0106         pad_tlv->len = new_pad - 2;
0107 
0108         err = bpf_lwt_seg6_store_bytes(skb, pad_off,
0109                            (void *)pad_tlv_buf, new_pad);
0110         if (err)
0111             return err;
0112     }
0113 
0114     return 0;
0115 }
0116 
0117 static __always_inline
0118 int is_valid_tlv_boundary(struct __sk_buff *skb, struct ip6_srh_t *srh,
0119               uint32_t *tlv_off, uint32_t *pad_size,
0120               uint32_t *pad_off)
0121 {
0122     uint32_t srh_off, cur_off;
0123     int offset_valid = 0;
0124     int err;
0125 
0126     srh_off = (char *)srh - (char *)(long)skb->data;
0127     // cur_off = end of segments, start of possible TLVs
0128     cur_off = srh_off + sizeof(*srh) +
0129         sizeof(struct ip6_addr_t) * (srh->first_segment + 1);
0130 
0131     *pad_off = 0;
0132 
0133     // we can only go as far as ~10 TLVs due to the BPF max stack size
0134     #pragma clang loop unroll(full)
0135     for (int i = 0; i < 10; i++) {
0136         struct sr6_tlv_t tlv;
0137 
0138         if (cur_off == *tlv_off)
0139             offset_valid = 1;
0140 
0141         if (cur_off >= srh_off + ((srh->hdrlen + 1) << 3))
0142             break;
0143 
0144         err = bpf_skb_load_bytes(skb, cur_off, &tlv, sizeof(tlv));
0145         if (err)
0146             return err;
0147 
0148         if (tlv.type == SR6_TLV_PADDING) {
0149             *pad_size = tlv.len + sizeof(tlv);
0150             *pad_off = cur_off;
0151 
0152             if (*tlv_off == srh_off) {
0153                 *tlv_off = cur_off;
0154                 offset_valid = 1;
0155             }
0156             break;
0157 
0158         } else if (tlv.type == SR6_TLV_HMAC) {
0159             break;
0160         }
0161 
0162         cur_off += sizeof(tlv) + tlv.len;
0163     } // we reached the padding or HMAC TLVs, or the end of the SRH
0164 
0165     if (*pad_off == 0)
0166         *pad_off = cur_off;
0167 
0168     if (*tlv_off == -1)
0169         *tlv_off = cur_off;
0170     else if (!offset_valid)
0171         return -EINVAL;
0172 
0173     return 0;
0174 }
0175 
0176 static __always_inline
0177 int add_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh, uint32_t tlv_off,
0178         struct sr6_tlv_t *itlv, uint8_t tlv_size)
0179 {
0180     uint32_t srh_off = (char *)srh - (char *)(long)skb->data;
0181     uint8_t len_remaining, new_pad;
0182     uint32_t pad_off = 0;
0183     uint32_t pad_size = 0;
0184     uint32_t partial_srh_len;
0185     int err;
0186 
0187     if (tlv_off != -1)
0188         tlv_off += srh_off;
0189 
0190     if (itlv->type == SR6_TLV_PADDING || itlv->type == SR6_TLV_HMAC)
0191         return -EINVAL;
0192 
0193     err = is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off);
0194     if (err)
0195         return err;
0196 
0197     err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, sizeof(*itlv) + itlv->len);
0198     if (err)
0199         return err;
0200 
0201     err = bpf_lwt_seg6_store_bytes(skb, tlv_off, (void *)itlv, tlv_size);
0202     if (err)
0203         return err;
0204 
0205     // the following can't be moved inside update_tlv_pad because the
0206     // bpf verifier has some issues with it
0207     pad_off += sizeof(*itlv) + itlv->len;
0208     partial_srh_len = pad_off - srh_off;
0209     len_remaining = partial_srh_len % 8;
0210     new_pad = 8 - len_remaining;
0211 
0212     if (new_pad == 1) // cannot pad for 1 byte only
0213         new_pad = 9;
0214     else if (new_pad == 8)
0215         new_pad = 0;
0216 
0217     return update_tlv_pad(skb, new_pad, pad_size, pad_off);
0218 }
0219 
0220 static __always_inline
0221 int delete_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh,
0222            uint32_t tlv_off)
0223 {
0224     uint32_t srh_off = (char *)srh - (char *)(long)skb->data;
0225     uint8_t len_remaining, new_pad;
0226     uint32_t partial_srh_len;
0227     uint32_t pad_off = 0;
0228     uint32_t pad_size = 0;
0229     struct sr6_tlv_t tlv;
0230     int err;
0231 
0232     tlv_off += srh_off;
0233 
0234     err = is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off);
0235     if (err)
0236         return err;
0237 
0238     err = bpf_skb_load_bytes(skb, tlv_off, &tlv, sizeof(tlv));
0239     if (err)
0240         return err;
0241 
0242     err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, -(sizeof(tlv) + tlv.len));
0243     if (err)
0244         return err;
0245 
0246     pad_off -= sizeof(tlv) + tlv.len;
0247     partial_srh_len = pad_off - srh_off;
0248     len_remaining = partial_srh_len % 8;
0249     new_pad = 8 - len_remaining;
0250     if (new_pad == 1) // cannot pad for 1 byte only
0251         new_pad = 9;
0252     else if (new_pad == 8)
0253         new_pad = 0;
0254 
0255     return update_tlv_pad(skb, new_pad, pad_size, pad_off);
0256 }
0257 
0258 static __always_inline
0259 int has_egr_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh)
0260 {
0261     int tlv_offset = sizeof(struct ip6_t) + sizeof(struct ip6_srh_t) +
0262         ((srh->first_segment + 1) << 4);
0263     struct sr6_tlv_t tlv;
0264 
0265     if (bpf_skb_load_bytes(skb, tlv_offset, &tlv, sizeof(struct sr6_tlv_t)))
0266         return 0;
0267 
0268     if (tlv.type == SR6_TLV_EGRESS && tlv.len == 18) {
0269         struct ip6_addr_t egr_addr;
0270 
0271         if (bpf_skb_load_bytes(skb, tlv_offset + 4, &egr_addr, 16))
0272             return 0;
0273 
0274         // check if egress TLV value is correct
0275         if (bpf_be64_to_cpu(egr_addr.hi) == 0xfd00000000000000 &&
0276             bpf_be64_to_cpu(egr_addr.lo) == 0x4)
0277             return 1;
0278     }
0279 
0280     return 0;
0281 }
0282 
0283 // This function will push a SRH with segments fd00::1, fd00::2, fd00::3,
0284 // fd00::4
0285 SEC("encap_srh")
0286 int __encap_srh(struct __sk_buff *skb)
0287 {
0288     unsigned long long hi = 0xfd00000000000000;
0289     struct ip6_addr_t *seg;
0290     struct ip6_srh_t *srh;
0291     char srh_buf[72]; // room for 4 segments
0292     int err;
0293 
0294     srh = (struct ip6_srh_t *)srh_buf;
0295     srh->nexthdr = 0;
0296     srh->hdrlen = 8;
0297     srh->type = 4;
0298     srh->segments_left = 3;
0299     srh->first_segment = 3;
0300     srh->flags = 0;
0301     srh->tag = 0;
0302 
0303     seg = (struct ip6_addr_t *)((char *)srh + sizeof(*srh));
0304 
0305     #pragma clang loop unroll(full)
0306     for (unsigned long long lo = 0; lo < 4; lo++) {
0307         seg->lo = bpf_cpu_to_be64(4 - lo);
0308         seg->hi = bpf_cpu_to_be64(hi);
0309         seg = (struct ip6_addr_t *)((char *)seg + sizeof(*seg));
0310     }
0311 
0312     err = bpf_lwt_push_encap(skb, 0, (void *)srh, sizeof(srh_buf));
0313     if (err)
0314         return BPF_DROP;
0315 
0316     return BPF_REDIRECT;
0317 }
0318 
0319 // Add an Egress TLV fc00::4, add the flag A,
0320 // and apply End.X action to fc42::1
0321 SEC("add_egr_x")
0322 int __add_egr_x(struct __sk_buff *skb)
0323 {
0324     unsigned long long hi = 0xfc42000000000000;
0325     unsigned long long lo = 0x1;
0326     struct ip6_srh_t *srh = get_srh(skb);
0327     uint8_t new_flags = SR6_FLAG_ALERT;
0328     struct ip6_addr_t addr;
0329     int err, offset;
0330 
0331     if (srh == NULL)
0332         return BPF_DROP;
0333 
0334     uint8_t tlv[20] = {2, 18, 0, 0, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0335                0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4};
0336 
0337     err = add_tlv(skb, srh, (srh->hdrlen+1) << 3,
0338               (struct sr6_tlv_t *)&tlv, 20);
0339     if (err)
0340         return BPF_DROP;
0341 
0342     offset = sizeof(struct ip6_t) + offsetof(struct ip6_srh_t, flags);
0343     err = bpf_lwt_seg6_store_bytes(skb, offset,
0344                        (void *)&new_flags, sizeof(new_flags));
0345     if (err)
0346         return BPF_DROP;
0347 
0348     addr.lo = bpf_cpu_to_be64(lo);
0349     addr.hi = bpf_cpu_to_be64(hi);
0350     err = bpf_lwt_seg6_action(skb, SEG6_LOCAL_ACTION_END_X,
0351                   (void *)&addr, sizeof(addr));
0352     if (err)
0353         return BPF_DROP;
0354     return BPF_REDIRECT;
0355 }
0356 
0357 // Pop the Egress TLV, reset the flags, change the tag 2442 and finally do a
0358 // simple End action
0359 SEC("pop_egr")
0360 int __pop_egr(struct __sk_buff *skb)
0361 {
0362     struct ip6_srh_t *srh = get_srh(skb);
0363     uint16_t new_tag = bpf_htons(2442);
0364     uint8_t new_flags = 0;
0365     int err, offset;
0366 
0367     if (srh == NULL)
0368         return BPF_DROP;
0369 
0370     if (srh->flags != SR6_FLAG_ALERT)
0371         return BPF_DROP;
0372 
0373     if (srh->hdrlen != 11) // 4 segments + Egress TLV + Padding TLV
0374         return BPF_DROP;
0375 
0376     if (!has_egr_tlv(skb, srh))
0377         return BPF_DROP;
0378 
0379     err = delete_tlv(skb, srh, 8 + (srh->first_segment + 1) * 16);
0380     if (err)
0381         return BPF_DROP;
0382 
0383     offset = sizeof(struct ip6_t) + offsetof(struct ip6_srh_t, flags);
0384     if (bpf_lwt_seg6_store_bytes(skb, offset, (void *)&new_flags,
0385                      sizeof(new_flags)))
0386         return BPF_DROP;
0387 
0388     offset = sizeof(struct ip6_t) + offsetof(struct ip6_srh_t, tag);
0389     if (bpf_lwt_seg6_store_bytes(skb, offset, (void *)&new_tag,
0390                      sizeof(new_tag)))
0391         return BPF_DROP;
0392 
0393     return BPF_OK;
0394 }
0395 
0396 // Inspect if the Egress TLV and flag have been removed, if the tag is correct,
0397 // then apply a End.T action to reach the last segment
0398 SEC("inspect_t")
0399 int __inspect_t(struct __sk_buff *skb)
0400 {
0401     struct ip6_srh_t *srh = get_srh(skb);
0402     int table = 117;
0403     int err;
0404 
0405     if (srh == NULL)
0406         return BPF_DROP;
0407 
0408     if (srh->flags != 0)
0409         return BPF_DROP;
0410 
0411     if (srh->tag != bpf_htons(2442))
0412         return BPF_DROP;
0413 
0414     if (srh->hdrlen != 8) // 4 segments
0415         return BPF_DROP;
0416 
0417     err = bpf_lwt_seg6_action(skb, SEG6_LOCAL_ACTION_END_T,
0418                   (void *)&table, sizeof(table));
0419 
0420     if (err)
0421         return BPF_DROP;
0422 
0423     return BPF_REDIRECT;
0424 }
0425 
0426 char __license[] SEC("license") = "GPL";