lwt_bpf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
  3. */
  4. #include <linux/filter.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/types.h>
  9. #include <linux/bpf.h>
  10. #include <net/lwtunnel.h>
  11. #include <net/gre.h>
  12. #include <net/ip6_route.h>
  13. #include <net/ipv6_stubs.h>
  14. #include <net/inet_dscp.h>
  15. struct bpf_lwt_prog {
  16. struct bpf_prog *prog;
  17. char *name;
  18. };
  19. struct bpf_lwt {
  20. struct bpf_lwt_prog in;
  21. struct bpf_lwt_prog out;
  22. struct bpf_lwt_prog xmit;
  23. int family;
  24. };
  25. #define MAX_PROG_NAME 256
  26. static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
  27. {
  28. return (struct bpf_lwt *)lwt->data;
  29. }
  30. #define NO_REDIRECT false
  31. #define CAN_REDIRECT true
  32. static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
  33. struct dst_entry *dst, bool can_redirect)
  34. {
  35. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  36. int ret;
  37. /* Disabling BH is needed to protect per-CPU bpf_redirect_info between
  38. * BPF prog and skb_do_redirect().
  39. */
  40. local_bh_disable();
  41. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  42. bpf_compute_data_pointers(skb);
  43. ret = bpf_prog_run_save_cb(lwt->prog, skb);
  44. switch (ret) {
  45. case BPF_OK:
  46. case BPF_LWT_REROUTE:
  47. break;
  48. case BPF_REDIRECT:
  49. if (unlikely(!can_redirect)) {
  50. pr_warn_once("Illegal redirect return code in prog %s\n",
  51. lwt->name ? : "<unknown>");
  52. ret = BPF_OK;
  53. } else {
  54. skb_reset_mac_header(skb);
  55. skb_do_redirect(skb);
  56. ret = BPF_REDIRECT;
  57. }
  58. break;
  59. case BPF_DROP:
  60. kfree_skb(skb);
  61. ret = -EPERM;
  62. break;
  63. default:
  64. pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
  65. kfree_skb(skb);
  66. ret = -EINVAL;
  67. break;
  68. }
  69. bpf_net_ctx_clear(bpf_net_ctx);
  70. local_bh_enable();
  71. return ret;
  72. }
  73. static int bpf_lwt_input_reroute(struct sk_buff *skb)
  74. {
  75. int err = -EINVAL;
  76. if (skb->protocol == htons(ETH_P_IP)) {
  77. struct net_device *dev = skb_dst(skb)->dev;
  78. struct iphdr *iph = ip_hdr(skb);
  79. dev_hold(dev);
  80. skb_dst_drop(skb);
  81. err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
  82. iph->tos, dev);
  83. dev_put(dev);
  84. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  85. skb_dst_drop(skb);
  86. err = ipv6_stub->ipv6_route_input(skb);
  87. } else {
  88. err = -EAFNOSUPPORT;
  89. }
  90. if (err)
  91. goto err;
  92. return dst_input(skb);
  93. err:
  94. kfree_skb(skb);
  95. return err;
  96. }
  97. static int bpf_input(struct sk_buff *skb)
  98. {
  99. struct dst_entry *dst = skb_dst(skb);
  100. struct bpf_lwt *bpf;
  101. int ret;
  102. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  103. if (bpf->in.prog) {
  104. ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
  105. if (ret < 0)
  106. return ret;
  107. if (ret == BPF_LWT_REROUTE)
  108. return bpf_lwt_input_reroute(skb);
  109. }
  110. if (unlikely(!dst->lwtstate->orig_input)) {
  111. kfree_skb(skb);
  112. return -EINVAL;
  113. }
  114. return dst->lwtstate->orig_input(skb);
  115. }
  116. static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  117. {
  118. struct dst_entry *dst = skb_dst(skb);
  119. struct bpf_lwt *bpf;
  120. int ret;
  121. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  122. if (bpf->out.prog) {
  123. ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. if (unlikely(!dst->lwtstate->orig_output)) {
  128. pr_warn_once("orig_output not set on dst for prog %s\n",
  129. bpf->out.name);
  130. kfree_skb(skb);
  131. return -EINVAL;
  132. }
  133. return dst->lwtstate->orig_output(net, sk, skb);
  134. }
  135. static int xmit_check_hhlen(struct sk_buff *skb, int hh_len)
  136. {
  137. if (skb_headroom(skb) < hh_len) {
  138. int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
  139. if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
  140. return -ENOMEM;
  141. }
  142. return 0;
  143. }
  144. static int bpf_lwt_xmit_reroute(struct sk_buff *skb)
  145. {
  146. struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev);
  147. int oif = l3mdev ? l3mdev->ifindex : 0;
  148. struct dst_entry *dst = NULL;
  149. int err = -EAFNOSUPPORT;
  150. struct sock *sk;
  151. struct net *net;
  152. bool ipv4;
  153. if (skb->protocol == htons(ETH_P_IP))
  154. ipv4 = true;
  155. else if (skb->protocol == htons(ETH_P_IPV6))
  156. ipv4 = false;
  157. else
  158. goto err;
  159. sk = sk_to_full_sk(skb->sk);
  160. if (sk) {
  161. if (sk->sk_bound_dev_if)
  162. oif = sk->sk_bound_dev_if;
  163. net = sock_net(sk);
  164. } else {
  165. net = dev_net(skb_dst(skb)->dev);
  166. }
  167. if (ipv4) {
  168. struct iphdr *iph = ip_hdr(skb);
  169. struct flowi4 fl4 = {};
  170. struct rtable *rt;
  171. fl4.flowi4_oif = oif;
  172. fl4.flowi4_mark = skb->mark;
  173. fl4.flowi4_uid = sock_net_uid(net, sk);
  174. fl4.flowi4_tos = iph->tos & INET_DSCP_MASK;
  175. fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
  176. fl4.flowi4_proto = iph->protocol;
  177. fl4.daddr = iph->daddr;
  178. fl4.saddr = iph->saddr;
  179. rt = ip_route_output_key(net, &fl4);
  180. if (IS_ERR(rt)) {
  181. err = PTR_ERR(rt);
  182. goto err;
  183. }
  184. dst = &rt->dst;
  185. } else {
  186. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  187. struct flowi6 fl6 = {};
  188. fl6.flowi6_oif = oif;
  189. fl6.flowi6_mark = skb->mark;
  190. fl6.flowi6_uid = sock_net_uid(net, sk);
  191. fl6.flowlabel = ip6_flowinfo(iph6);
  192. fl6.flowi6_proto = iph6->nexthdr;
  193. fl6.daddr = iph6->daddr;
  194. fl6.saddr = iph6->saddr;
  195. dst = ipv6_stub->ipv6_dst_lookup_flow(net, skb->sk, &fl6, NULL);
  196. if (IS_ERR(dst)) {
  197. err = PTR_ERR(dst);
  198. goto err;
  199. }
  200. }
  201. if (unlikely(dst->error)) {
  202. err = dst->error;
  203. dst_release(dst);
  204. goto err;
  205. }
  206. /* Although skb header was reserved in bpf_lwt_push_ip_encap(), it
  207. * was done for the previous dst, so we are doing it here again, in
  208. * case the new dst needs much more space. The call below is a noop
  209. * if there is enough header space in skb.
  210. */
  211. err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
  212. if (unlikely(err))
  213. goto err;
  214. skb_dst_drop(skb);
  215. skb_dst_set(skb, dst);
  216. err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  217. if (unlikely(err))
  218. return net_xmit_errno(err);
  219. /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */
  220. return LWTUNNEL_XMIT_DONE;
  221. err:
  222. kfree_skb(skb);
  223. return err;
  224. }
  225. static int bpf_xmit(struct sk_buff *skb)
  226. {
  227. struct dst_entry *dst = skb_dst(skb);
  228. struct bpf_lwt *bpf;
  229. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  230. if (bpf->xmit.prog) {
  231. int hh_len = dst->dev->hard_header_len;
  232. __be16 proto = skb->protocol;
  233. int ret;
  234. ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
  235. switch (ret) {
  236. case BPF_OK:
  237. /* If the header changed, e.g. via bpf_lwt_push_encap,
  238. * BPF_LWT_REROUTE below should have been used if the
  239. * protocol was also changed.
  240. */
  241. if (skb->protocol != proto) {
  242. kfree_skb(skb);
  243. return -EINVAL;
  244. }
  245. /* If the header was expanded, headroom might be too
  246. * small for L2 header to come, expand as needed.
  247. */
  248. ret = xmit_check_hhlen(skb, hh_len);
  249. if (unlikely(ret))
  250. return ret;
  251. return LWTUNNEL_XMIT_CONTINUE;
  252. case BPF_REDIRECT:
  253. return LWTUNNEL_XMIT_DONE;
  254. case BPF_LWT_REROUTE:
  255. return bpf_lwt_xmit_reroute(skb);
  256. default:
  257. return ret;
  258. }
  259. }
  260. return LWTUNNEL_XMIT_CONTINUE;
  261. }
  262. static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
  263. {
  264. if (prog->prog)
  265. bpf_prog_put(prog->prog);
  266. kfree(prog->name);
  267. }
  268. static void bpf_destroy_state(struct lwtunnel_state *lwt)
  269. {
  270. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  271. bpf_lwt_prog_destroy(&bpf->in);
  272. bpf_lwt_prog_destroy(&bpf->out);
  273. bpf_lwt_prog_destroy(&bpf->xmit);
  274. }
  275. static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
  276. [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
  277. [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  278. .len = MAX_PROG_NAME },
  279. };
  280. static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
  281. enum bpf_prog_type type)
  282. {
  283. struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
  284. struct bpf_prog *p;
  285. int ret;
  286. u32 fd;
  287. ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr,
  288. bpf_prog_policy, NULL);
  289. if (ret < 0)
  290. return ret;
  291. if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
  292. return -EINVAL;
  293. prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
  294. if (!prog->name)
  295. return -ENOMEM;
  296. fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
  297. p = bpf_prog_get_type(fd, type);
  298. if (IS_ERR(p))
  299. return PTR_ERR(p);
  300. prog->prog = p;
  301. return 0;
  302. }
  303. static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
  304. [LWT_BPF_IN] = { .type = NLA_NESTED, },
  305. [LWT_BPF_OUT] = { .type = NLA_NESTED, },
  306. [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
  307. [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
  308. };
  309. static int bpf_build_state(struct net *net, struct nlattr *nla,
  310. unsigned int family, const void *cfg,
  311. struct lwtunnel_state **ts,
  312. struct netlink_ext_ack *extack)
  313. {
  314. struct nlattr *tb[LWT_BPF_MAX + 1];
  315. struct lwtunnel_state *newts;
  316. struct bpf_lwt *bpf;
  317. int ret;
  318. if (family != AF_INET && family != AF_INET6)
  319. return -EAFNOSUPPORT;
  320. ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy,
  321. extack);
  322. if (ret < 0)
  323. return ret;
  324. if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
  325. return -EINVAL;
  326. newts = lwtunnel_state_alloc(sizeof(*bpf));
  327. if (!newts)
  328. return -ENOMEM;
  329. newts->type = LWTUNNEL_ENCAP_BPF;
  330. bpf = bpf_lwt_lwtunnel(newts);
  331. if (tb[LWT_BPF_IN]) {
  332. newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
  333. ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
  334. BPF_PROG_TYPE_LWT_IN);
  335. if (ret < 0)
  336. goto errout;
  337. }
  338. if (tb[LWT_BPF_OUT]) {
  339. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  340. ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
  341. BPF_PROG_TYPE_LWT_OUT);
  342. if (ret < 0)
  343. goto errout;
  344. }
  345. if (tb[LWT_BPF_XMIT]) {
  346. newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
  347. ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
  348. BPF_PROG_TYPE_LWT_XMIT);
  349. if (ret < 0)
  350. goto errout;
  351. }
  352. if (tb[LWT_BPF_XMIT_HEADROOM]) {
  353. u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
  354. if (headroom > LWT_BPF_MAX_HEADROOM) {
  355. ret = -ERANGE;
  356. goto errout;
  357. }
  358. newts->headroom = headroom;
  359. }
  360. bpf->family = family;
  361. *ts = newts;
  362. return 0;
  363. errout:
  364. bpf_destroy_state(newts);
  365. kfree(newts);
  366. return ret;
  367. }
  368. static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
  369. struct bpf_lwt_prog *prog)
  370. {
  371. struct nlattr *nest;
  372. if (!prog->prog)
  373. return 0;
  374. nest = nla_nest_start_noflag(skb, attr);
  375. if (!nest)
  376. return -EMSGSIZE;
  377. if (prog->name &&
  378. nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
  379. return -EMSGSIZE;
  380. return nla_nest_end(skb, nest);
  381. }
  382. static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
  383. {
  384. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  385. if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
  386. bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
  387. bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
  388. return -EMSGSIZE;
  389. return 0;
  390. }
  391. static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
  392. {
  393. int nest_len = nla_total_size(sizeof(struct nlattr)) +
  394. nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
  395. 0;
  396. return nest_len + /* LWT_BPF_IN */
  397. nest_len + /* LWT_BPF_OUT */
  398. nest_len + /* LWT_BPF_XMIT */
  399. 0;
  400. }
  401. static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
  402. {
  403. /* FIXME:
  404. * The LWT state is currently rebuilt for delete requests which
  405. * results in a new bpf_prog instance. Comparing names for now.
  406. */
  407. if (!a->name && !b->name)
  408. return 0;
  409. if (!a->name || !b->name)
  410. return 1;
  411. return strcmp(a->name, b->name);
  412. }
  413. static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  414. {
  415. struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
  416. struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
  417. return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
  418. bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
  419. bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
  420. }
  421. static const struct lwtunnel_encap_ops bpf_encap_ops = {
  422. .build_state = bpf_build_state,
  423. .destroy_state = bpf_destroy_state,
  424. .input = bpf_input,
  425. .output = bpf_output,
  426. .xmit = bpf_xmit,
  427. .fill_encap = bpf_fill_encap_info,
  428. .get_encap_size = bpf_encap_nlsize,
  429. .cmp_encap = bpf_encap_cmp,
  430. .owner = THIS_MODULE,
  431. };
  432. static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
  433. int encap_len)
  434. {
  435. struct skb_shared_info *shinfo = skb_shinfo(skb);
  436. gso_type |= SKB_GSO_DODGY;
  437. shinfo->gso_type |= gso_type;
  438. skb_decrease_gso_size(shinfo, encap_len);
  439. shinfo->gso_segs = 0;
  440. return 0;
  441. }
  442. static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len)
  443. {
  444. int next_hdr_offset;
  445. void *next_hdr;
  446. __u8 protocol;
  447. /* SCTP and UDP_L4 gso need more nuanced handling than what
  448. * handle_gso_type() does above: skb_decrease_gso_size() is not enough.
  449. * So at the moment only TCP GSO packets are let through.
  450. */
  451. if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  452. return -ENOTSUPP;
  453. if (ipv4) {
  454. protocol = ip_hdr(skb)->protocol;
  455. next_hdr_offset = sizeof(struct iphdr);
  456. next_hdr = skb_network_header(skb) + next_hdr_offset;
  457. } else {
  458. protocol = ipv6_hdr(skb)->nexthdr;
  459. next_hdr_offset = sizeof(struct ipv6hdr);
  460. next_hdr = skb_network_header(skb) + next_hdr_offset;
  461. }
  462. switch (protocol) {
  463. case IPPROTO_GRE:
  464. next_hdr_offset += sizeof(struct gre_base_hdr);
  465. if (next_hdr_offset > encap_len)
  466. return -EINVAL;
  467. if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM)
  468. return handle_gso_type(skb, SKB_GSO_GRE_CSUM,
  469. encap_len);
  470. return handle_gso_type(skb, SKB_GSO_GRE, encap_len);
  471. case IPPROTO_UDP:
  472. next_hdr_offset += sizeof(struct udphdr);
  473. if (next_hdr_offset > encap_len)
  474. return -EINVAL;
  475. if (((struct udphdr *)next_hdr)->check)
  476. return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM,
  477. encap_len);
  478. return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len);
  479. case IPPROTO_IP:
  480. case IPPROTO_IPV6:
  481. if (ipv4)
  482. return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len);
  483. else
  484. return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len);
  485. default:
  486. return -EPROTONOSUPPORT;
  487. }
  488. }
  489. int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
  490. {
  491. struct iphdr *iph;
  492. bool ipv4;
  493. int err;
  494. if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
  495. return -EINVAL;
  496. /* validate protocol and length */
  497. iph = (struct iphdr *)hdr;
  498. if (iph->version == 4) {
  499. ipv4 = true;
  500. if (unlikely(len < iph->ihl * 4))
  501. return -EINVAL;
  502. } else if (iph->version == 6) {
  503. ipv4 = false;
  504. if (unlikely(len < sizeof(struct ipv6hdr)))
  505. return -EINVAL;
  506. } else {
  507. return -EINVAL;
  508. }
  509. if (ingress)
  510. err = skb_cow_head(skb, len + skb->mac_len);
  511. else
  512. err = skb_cow_head(skb,
  513. len + LL_RESERVED_SPACE(skb_dst(skb)->dev));
  514. if (unlikely(err))
  515. return err;
  516. /* push the encap headers and fix pointers */
  517. skb_reset_inner_headers(skb);
  518. skb_reset_inner_mac_header(skb); /* mac header is not yet set */
  519. skb_set_inner_protocol(skb, skb->protocol);
  520. skb->encapsulation = 1;
  521. skb_push(skb, len);
  522. if (ingress)
  523. skb_postpush_rcsum(skb, iph, len);
  524. skb_reset_network_header(skb);
  525. memcpy(skb_network_header(skb), hdr, len);
  526. bpf_compute_data_pointers(skb);
  527. skb_clear_hash(skb);
  528. if (ipv4) {
  529. skb->protocol = htons(ETH_P_IP);
  530. iph = ip_hdr(skb);
  531. if (!iph->check)
  532. iph->check = ip_fast_csum((unsigned char *)iph,
  533. iph->ihl);
  534. } else {
  535. skb->protocol = htons(ETH_P_IPV6);
  536. }
  537. if (skb_is_gso(skb))
  538. return handle_gso_encap(skb, ipv4, len);
  539. return 0;
  540. }
  541. static int __init bpf_lwt_init(void)
  542. {
  543. return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
  544. }
  545. subsys_initcall(bpf_lwt_init)