1 #include <stddef.h> 2 #include <inttypes.h> 3 #include <errno.h> 4 #include <linux/seg6_local.h> 5 #include <linux/bpf.h> 6 #include "bpf_helpers.h" 7 #include "bpf_endian.h" 8 9 /* Packet parsing state machine helpers. */ 10 #define cursor_advance(_cursor, _len) \ 11 ({ void *_tmp = _cursor; _cursor += _len; _tmp; }) 12 13 #define SR6_FLAG_ALERT (1 << 4) 14 15 #define htonll(x) ((bpf_htonl(1)) == 1 ? (x) : ((uint64_t)bpf_htonl((x) & \ 16 0xFFFFFFFF) << 32) | bpf_htonl((x) >> 32)) 17 #define ntohll(x) ((bpf_ntohl(1)) == 1 ? (x) : ((uint64_t)bpf_ntohl((x) & \ 18 0xFFFFFFFF) << 32) | bpf_ntohl((x) >> 32)) 19 #define BPF_PACKET_HEADER __attribute__((packed)) 20 21 struct ip6_t { 22 unsigned int ver:4; 23 unsigned int priority:8; 24 unsigned int flow_label:20; 25 unsigned short payload_len; 26 unsigned char next_header; 27 unsigned char hop_limit; 28 unsigned long long src_hi; 29 unsigned long long src_lo; 30 unsigned long long dst_hi; 31 unsigned long long dst_lo; 32 } BPF_PACKET_HEADER; 33 34 struct ip6_addr_t { 35 unsigned long long hi; 36 unsigned long long lo; 37 } BPF_PACKET_HEADER; 38 39 struct ip6_srh_t { 40 unsigned char nexthdr; 41 unsigned char hdrlen; 42 unsigned char type; 43 unsigned char segments_left; 44 unsigned char first_segment; 45 unsigned char flags; 46 unsigned short tag; 47 48 struct ip6_addr_t segments[0]; 49 } BPF_PACKET_HEADER; 50 51 struct sr6_tlv_t { 52 unsigned char type; 53 unsigned char len; 54 unsigned char value[0]; 55 } BPF_PACKET_HEADER; 56 57 static __attribute__((always_inline)) struct ip6_srh_t *get_srh(struct __sk_buff *skb) 58 { 59 void *cursor, *data_end; 60 struct ip6_srh_t *srh; 61 struct ip6_t *ip; 62 uint8_t *ipver; 63 64 data_end = (void *)(long)skb->data_end; 65 cursor = (void *)(long)skb->data; 66 ipver = (uint8_t *)cursor; 67 68 if ((void *)ipver + sizeof(*ipver) > data_end) 69 return NULL; 70 71 if ((*ipver >> 4) != 6) 72 return NULL; 73 74 ip = cursor_advance(cursor, sizeof(*ip)); 75 if ((void *)ip + sizeof(*ip) > data_end) 76 return NULL; 77 78 if (ip->next_header != 43) 79 return NULL; 80 81 srh = cursor_advance(cursor, sizeof(*srh)); 82 if ((void *)srh + sizeof(*srh) > data_end) 83 return NULL; 84 85 if (srh->type != 4) 86 return NULL; 87 88 return srh; 89 } 90 91 static __attribute__((always_inline)) 92 int update_tlv_pad(struct __sk_buff *skb, uint32_t new_pad, 93 uint32_t old_pad, uint32_t pad_off) 94 { 95 int err; 96 97 if (new_pad != old_pad) { 98 err = bpf_lwt_seg6_adjust_srh(skb, pad_off, 99 (int) new_pad - (int) old_pad); 100 if (err) 101 return err; 102 } 103 104 if (new_pad > 0) { 105 char pad_tlv_buf[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 106 0, 0, 0}; 107 struct sr6_tlv_t *pad_tlv = (struct sr6_tlv_t *) pad_tlv_buf; 108 109 pad_tlv->type = SR6_TLV_PADDING; 110 pad_tlv->len = new_pad - 2; 111 112 err = bpf_lwt_seg6_store_bytes(skb, pad_off, 113 (void *)pad_tlv_buf, new_pad); 114 if (err) 115 return err; 116 } 117 118 return 0; 119 } 120 121 static __attribute__((always_inline)) 122 int is_valid_tlv_boundary(struct __sk_buff *skb, struct ip6_srh_t *srh, 123 uint32_t *tlv_off, uint32_t *pad_size, 124 uint32_t *pad_off) 125 { 126 uint32_t srh_off, cur_off; 127 int offset_valid = 0; 128 int err; 129 130 srh_off = (char *)srh - (char *)(long)skb->data; 131 // cur_off = end of segments, start of possible TLVs 132 cur_off = srh_off + sizeof(*srh) + 133 sizeof(struct ip6_addr_t) * (srh->first_segment + 1); 134 135 *pad_off = 0; 136 137 // we can only go as far as ~10 TLVs due to the BPF max stack size 138 #pragma clang loop unroll(disable) 139 for (int i = 0; i < 100; i++) { 140 struct sr6_tlv_t tlv; 141 142 if (cur_off == *tlv_off) 143 offset_valid = 1; 144 145 if (cur_off >= srh_off + ((srh->hdrlen + 1) << 3)) 146 break; 147 148 err = bpf_skb_load_bytes(skb, cur_off, &tlv, sizeof(tlv)); 149 if (err) 150 return err; 151 152 if (tlv.type == SR6_TLV_PADDING) { 153 *pad_size = tlv.len + sizeof(tlv); 154 *pad_off = cur_off; 155 156 if (*tlv_off == srh_off) { 157 *tlv_off = cur_off; 158 offset_valid = 1; 159 } 160 break; 161 162 } else if (tlv.type == SR6_TLV_HMAC) { 163 break; 164 } 165 166 cur_off += sizeof(tlv) + tlv.len; 167 } // we reached the padding or HMAC TLVs, or the end of the SRH 168 169 if (*pad_off == 0) 170 *pad_off = cur_off; 171 172 if (*tlv_off == -1) 173 *tlv_off = cur_off; 174 else if (!offset_valid) 175 return -EINVAL; 176 177 return 0; 178 } 179 180 static __attribute__((always_inline)) 181 int add_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh, uint32_t tlv_off, 182 struct sr6_tlv_t *itlv, uint8_t tlv_size) 183 { 184 uint32_t srh_off = (char *)srh - (char *)(long)skb->data; 185 uint8_t len_remaining, new_pad; 186 uint32_t pad_off = 0; 187 uint32_t pad_size = 0; 188 uint32_t partial_srh_len; 189 int err; 190 191 if (tlv_off != -1) 192 tlv_off += srh_off; 193 194 if (itlv->type == SR6_TLV_PADDING || itlv->type == SR6_TLV_HMAC) 195 return -EINVAL; 196 197 err = is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off); 198 if (err) 199 return err; 200 201 err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, sizeof(*itlv) + itlv->len); 202 if (err) 203 return err; 204 205 err = bpf_lwt_seg6_store_bytes(skb, tlv_off, (void *)itlv, tlv_size); 206 if (err) 207 return err; 208 209 // the following can't be moved inside update_tlv_pad because the 210 // bpf verifier has some issues with it 211 pad_off += sizeof(*itlv) + itlv->len; 212 partial_srh_len = pad_off - srh_off; 213 len_remaining = partial_srh_len % 8; 214 new_pad = 8 - len_remaining; 215 216 if (new_pad == 1) // cannot pad for 1 byte only 217 new_pad = 9; 218 else if (new_pad == 8) 219 new_pad = 0; 220 221 return update_tlv_pad(skb, new_pad, pad_size, pad_off); 222 } 223 224 // Add an Egress TLV fc00::4, add the flag A, 225 // and apply End.X action to fc42::1 226 SEC("lwt_seg6local") 227 int __add_egr_x(struct __sk_buff *skb) 228 { 229 unsigned long long hi = 0xfc42000000000000; 230 unsigned long long lo = 0x1; 231 struct ip6_srh_t *srh = get_srh(skb); 232 uint8_t new_flags = SR6_FLAG_ALERT; 233 struct ip6_addr_t addr; 234 int err, offset; 235 236 if (srh == NULL) 237 return BPF_DROP; 238 239 uint8_t tlv[20] = {2, 18, 0, 0, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 240 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4}; 241 242 err = add_tlv(skb, srh, (srh->hdrlen+1) << 3, 243 (struct sr6_tlv_t *)&tlv, 20); 244 if (err) 245 return BPF_DROP; 246 247 offset = sizeof(struct ip6_t) + offsetof(struct ip6_srh_t, flags); 248 err = bpf_lwt_seg6_store_bytes(skb, offset, 249 (void *)&new_flags, sizeof(new_flags)); 250 if (err) 251 return BPF_DROP; 252 253 addr.lo = htonll(lo); 254 addr.hi = htonll(hi); 255 err = bpf_lwt_seg6_action(skb, SEG6_LOCAL_ACTION_END_X, 256 (void *)&addr, sizeof(addr)); 257 if (err) 258 return BPF_DROP; 259 return BPF_REDIRECT; 260 } 261 char __license[] SEC("license") = "GPL"; 262