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 int update_tlv_pad(struct __sk_buff *skb,
0088                       uint32_t new_pad, uint32_t old_pad,
0089                       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 int is_valid_tlv_boundary(struct __sk_buff *skb,
0118                          struct ip6_srh_t *srh,
0119                          uint32_t *tlv_off,
0120                          uint32_t *pad_size,
0121                          uint32_t *pad_off)
0122 {
0123     uint32_t srh_off, cur_off;
0124     int offset_valid = 0;
0125     int err;
0126 
0127     srh_off = (char *)srh - (char *)(long)skb->data;
0128     // cur_off = end of segments, start of possible TLVs
0129     cur_off = srh_off + sizeof(*srh) +
0130         sizeof(struct ip6_addr_t) * (srh->first_segment + 1);
0131 
0132     *pad_off = 0;
0133 
0134     // we can only go as far as ~10 TLVs due to the BPF max stack size
0135     // workaround: define induction variable "i" as "long" instead
0136     // of "int" to prevent alu32 sub-register spilling.
0137     #pragma clang loop unroll(disable)
0138     for (long i = 0; i < 100; i++) {
0139         struct sr6_tlv_t tlv;
0140 
0141         if (cur_off == *tlv_off)
0142             offset_valid = 1;
0143 
0144         if (cur_off >= srh_off + ((srh->hdrlen + 1) << 3))
0145             break;
0146 
0147         err = bpf_skb_load_bytes(skb, cur_off, &tlv, sizeof(tlv));
0148         if (err)
0149             return err;
0150 
0151         if (tlv.type == SR6_TLV_PADDING) {
0152             *pad_size = tlv.len + sizeof(tlv);
0153             *pad_off = cur_off;
0154 
0155             if (*tlv_off == srh_off) {
0156                 *tlv_off = cur_off;
0157                 offset_valid = 1;
0158             }
0159             break;
0160 
0161         } else if (tlv.type == SR6_TLV_HMAC) {
0162             break;
0163         }
0164 
0165         cur_off += sizeof(tlv) + tlv.len;
0166     } // we reached the padding or HMAC TLVs, or the end of the SRH
0167 
0168     if (*pad_off == 0)
0169         *pad_off = cur_off;
0170 
0171     if (*tlv_off == -1)
0172         *tlv_off = cur_off;
0173     else if (!offset_valid)
0174         return -EINVAL;
0175 
0176     return 0;
0177 }
0178 
0179 static __always_inline int add_tlv(struct __sk_buff *skb,
0180                    struct ip6_srh_t *srh, uint32_t tlv_off,
0181                    struct sr6_tlv_t *itlv, uint8_t tlv_size)
0182 {
0183     uint32_t srh_off = (char *)srh - (char *)(long)skb->data;
0184     uint8_t len_remaining, new_pad;
0185     uint32_t pad_off = 0;
0186     uint32_t pad_size = 0;
0187     uint32_t partial_srh_len;
0188     int err;
0189 
0190     if (tlv_off != -1)
0191         tlv_off += srh_off;
0192 
0193     if (itlv->type == SR6_TLV_PADDING || itlv->type == SR6_TLV_HMAC)
0194         return -EINVAL;
0195 
0196     err = is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off);
0197     if (err)
0198         return err;
0199 
0200     err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, sizeof(*itlv) + itlv->len);
0201     if (err)
0202         return err;
0203 
0204     err = bpf_lwt_seg6_store_bytes(skb, tlv_off, (void *)itlv, tlv_size);
0205     if (err)
0206         return err;
0207 
0208     // the following can't be moved inside update_tlv_pad because the
0209     // bpf verifier has some issues with it
0210     pad_off += sizeof(*itlv) + itlv->len;
0211     partial_srh_len = pad_off - srh_off;
0212     len_remaining = partial_srh_len % 8;
0213     new_pad = 8 - len_remaining;
0214 
0215     if (new_pad == 1) // cannot pad for 1 byte only
0216         new_pad = 9;
0217     else if (new_pad == 8)
0218         new_pad = 0;
0219 
0220     return update_tlv_pad(skb, new_pad, pad_size, pad_off);
0221 }
0222 
0223 // Add an Egress TLV fc00::4, add the flag A,
0224 // and apply End.X action to fc42::1
0225 SEC("lwt_seg6local")
0226 int __add_egr_x(struct __sk_buff *skb)
0227 {
0228     unsigned long long hi = 0xfc42000000000000;
0229     unsigned long long lo = 0x1;
0230     struct ip6_srh_t *srh = get_srh(skb);
0231     uint8_t new_flags = SR6_FLAG_ALERT;
0232     struct ip6_addr_t addr;
0233     int err, offset;
0234 
0235     if (srh == NULL)
0236         return BPF_DROP;
0237 
0238     uint8_t tlv[20] = {2, 18, 0, 0, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0239                0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4};
0240 
0241     err = add_tlv(skb, srh, (srh->hdrlen+1) << 3,
0242               (struct sr6_tlv_t *)&tlv, 20);
0243     if (err)
0244         return BPF_DROP;
0245 
0246     offset = sizeof(struct ip6_t) + offsetof(struct ip6_srh_t, flags);
0247     err = bpf_lwt_seg6_store_bytes(skb, offset,
0248                        (void *)&new_flags, sizeof(new_flags));
0249     if (err)
0250         return BPF_DROP;
0251 
0252     addr.lo = bpf_cpu_to_be64(lo);
0253     addr.hi = bpf_cpu_to_be64(hi);
0254     err = bpf_lwt_seg6_action(skb, SEG6_LOCAL_ACTION_END_X,
0255                   (void *)&addr, sizeof(addr));
0256     if (err)
0257         return BPF_DROP;
0258     return BPF_REDIRECT;
0259 }
0260 char __license[] SEC("license") = "GPL";