seg6_local.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * SR-IPv6 implementation
  3. *
  4. * Authors:
  5. * David Lebrun <david.lebrun@uclouvain.be>
  6. * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/net.h>
  17. #include <linux/module.h>
  18. #include <net/ip.h>
  19. #include <net/lwtunnel.h>
  20. #include <net/netevent.h>
  21. #include <net/netns/generic.h>
  22. #include <net/ip6_fib.h>
  23. #include <net/route.h>
  24. #include <net/seg6.h>
  25. #include <linux/seg6.h>
  26. #include <linux/seg6_local.h>
  27. #include <net/addrconf.h>
  28. #include <net/ip6_route.h>
  29. #include <net/dst_cache.h>
  30. #include <net/ip_tunnels.h>
  31. #ifdef CONFIG_IPV6_SEG6_HMAC
  32. #include <net/seg6_hmac.h>
  33. #endif
  34. #include <net/seg6_local.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/bpf.h>
  37. struct seg6_local_lwt;
  38. struct seg6_action_desc {
  39. int action;
  40. unsigned long attrs;
  41. int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  42. int static_headroom;
  43. };
  44. struct bpf_lwt_prog {
  45. struct bpf_prog *prog;
  46. char *name;
  47. };
  48. struct seg6_local_lwt {
  49. int action;
  50. struct ipv6_sr_hdr *srh;
  51. int table;
  52. struct in_addr nh4;
  53. struct in6_addr nh6;
  54. int iif;
  55. int oif;
  56. struct bpf_lwt_prog bpf;
  57. int headroom;
  58. struct seg6_action_desc *desc;
  59. };
  60. static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
  61. {
  62. return (struct seg6_local_lwt *)lwt->data;
  63. }
  64. static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
  65. {
  66. struct ipv6_sr_hdr *srh;
  67. int len, srhoff = 0;
  68. if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
  69. return NULL;
  70. if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
  71. return NULL;
  72. srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
  73. len = (srh->hdrlen + 1) << 3;
  74. if (!pskb_may_pull(skb, srhoff + len))
  75. return NULL;
  76. if (!seg6_validate_srh(srh, len))
  77. return NULL;
  78. return srh;
  79. }
  80. static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
  81. {
  82. struct ipv6_sr_hdr *srh;
  83. srh = get_srh(skb);
  84. if (!srh)
  85. return NULL;
  86. if (srh->segments_left == 0)
  87. return NULL;
  88. #ifdef CONFIG_IPV6_SEG6_HMAC
  89. if (!seg6_hmac_validate_skb(skb))
  90. return NULL;
  91. #endif
  92. return srh;
  93. }
  94. static bool decap_and_validate(struct sk_buff *skb, int proto)
  95. {
  96. struct ipv6_sr_hdr *srh;
  97. unsigned int off = 0;
  98. srh = get_srh(skb);
  99. if (srh && srh->segments_left > 0)
  100. return false;
  101. #ifdef CONFIG_IPV6_SEG6_HMAC
  102. if (srh && !seg6_hmac_validate_skb(skb))
  103. return false;
  104. #endif
  105. if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
  106. return false;
  107. if (!pskb_pull(skb, off))
  108. return false;
  109. skb_postpull_rcsum(skb, skb_network_header(skb), off);
  110. skb_reset_network_header(skb);
  111. skb_reset_transport_header(skb);
  112. if (iptunnel_pull_offloads(skb))
  113. return false;
  114. return true;
  115. }
  116. static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
  117. {
  118. struct in6_addr *addr;
  119. srh->segments_left--;
  120. addr = srh->segments + srh->segments_left;
  121. *daddr = *addr;
  122. }
  123. int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
  124. u32 tbl_id)
  125. {
  126. struct net *net = dev_net(skb->dev);
  127. struct ipv6hdr *hdr = ipv6_hdr(skb);
  128. int flags = RT6_LOOKUP_F_HAS_SADDR;
  129. struct dst_entry *dst = NULL;
  130. struct rt6_info *rt;
  131. struct flowi6 fl6;
  132. fl6.flowi6_iif = skb->dev->ifindex;
  133. fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
  134. fl6.saddr = hdr->saddr;
  135. fl6.flowlabel = ip6_flowinfo(hdr);
  136. fl6.flowi6_mark = skb->mark;
  137. fl6.flowi6_proto = hdr->nexthdr;
  138. if (nhaddr)
  139. fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
  140. if (!tbl_id) {
  141. dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
  142. } else {
  143. struct fib6_table *table;
  144. table = fib6_get_table(net, tbl_id);
  145. if (!table)
  146. goto out;
  147. rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
  148. dst = &rt->dst;
  149. }
  150. if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
  151. dst_release(dst);
  152. dst = NULL;
  153. }
  154. out:
  155. if (!dst) {
  156. rt = net->ipv6.ip6_blk_hole_entry;
  157. dst = &rt->dst;
  158. dst_hold(dst);
  159. }
  160. skb_dst_drop(skb);
  161. skb_dst_set(skb, dst);
  162. return dst->error;
  163. }
  164. /* regular endpoint function */
  165. static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  166. {
  167. struct ipv6_sr_hdr *srh;
  168. srh = get_and_validate_srh(skb);
  169. if (!srh)
  170. goto drop;
  171. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  172. seg6_lookup_nexthop(skb, NULL, 0);
  173. return dst_input(skb);
  174. drop:
  175. kfree_skb(skb);
  176. return -EINVAL;
  177. }
  178. /* regular endpoint, and forward to specified nexthop */
  179. static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  180. {
  181. struct ipv6_sr_hdr *srh;
  182. srh = get_and_validate_srh(skb);
  183. if (!srh)
  184. goto drop;
  185. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  186. seg6_lookup_nexthop(skb, &slwt->nh6, 0);
  187. return dst_input(skb);
  188. drop:
  189. kfree_skb(skb);
  190. return -EINVAL;
  191. }
  192. static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  193. {
  194. struct ipv6_sr_hdr *srh;
  195. srh = get_and_validate_srh(skb);
  196. if (!srh)
  197. goto drop;
  198. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  199. seg6_lookup_nexthop(skb, NULL, slwt->table);
  200. return dst_input(skb);
  201. drop:
  202. kfree_skb(skb);
  203. return -EINVAL;
  204. }
  205. /* decapsulate and forward inner L2 frame on specified interface */
  206. static int input_action_end_dx2(struct sk_buff *skb,
  207. struct seg6_local_lwt *slwt)
  208. {
  209. struct net *net = dev_net(skb->dev);
  210. struct net_device *odev;
  211. struct ethhdr *eth;
  212. if (!decap_and_validate(skb, NEXTHDR_NONE))
  213. goto drop;
  214. if (!pskb_may_pull(skb, ETH_HLEN))
  215. goto drop;
  216. skb_reset_mac_header(skb);
  217. eth = (struct ethhdr *)skb->data;
  218. /* To determine the frame's protocol, we assume it is 802.3. This avoids
  219. * a call to eth_type_trans(), which is not really relevant for our
  220. * use case.
  221. */
  222. if (!eth_proto_is_802_3(eth->h_proto))
  223. goto drop;
  224. odev = dev_get_by_index_rcu(net, slwt->oif);
  225. if (!odev)
  226. goto drop;
  227. /* As we accept Ethernet frames, make sure the egress device is of
  228. * the correct type.
  229. */
  230. if (odev->type != ARPHRD_ETHER)
  231. goto drop;
  232. if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
  233. goto drop;
  234. skb_orphan(skb);
  235. if (skb_warn_if_lro(skb))
  236. goto drop;
  237. skb_forward_csum(skb);
  238. if (skb->len - ETH_HLEN > odev->mtu)
  239. goto drop;
  240. skb->dev = odev;
  241. skb->protocol = eth->h_proto;
  242. return dev_queue_xmit(skb);
  243. drop:
  244. kfree_skb(skb);
  245. return -EINVAL;
  246. }
  247. /* decapsulate and forward to specified nexthop */
  248. static int input_action_end_dx6(struct sk_buff *skb,
  249. struct seg6_local_lwt *slwt)
  250. {
  251. struct in6_addr *nhaddr = NULL;
  252. /* this function accepts IPv6 encapsulated packets, with either
  253. * an SRH with SL=0, or no SRH.
  254. */
  255. if (!decap_and_validate(skb, IPPROTO_IPV6))
  256. goto drop;
  257. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  258. goto drop;
  259. /* The inner packet is not associated to any local interface,
  260. * so we do not call netif_rx().
  261. *
  262. * If slwt->nh6 is set to ::, then lookup the nexthop for the
  263. * inner packet's DA. Otherwise, use the specified nexthop.
  264. */
  265. if (!ipv6_addr_any(&slwt->nh6))
  266. nhaddr = &slwt->nh6;
  267. seg6_lookup_nexthop(skb, nhaddr, 0);
  268. return dst_input(skb);
  269. drop:
  270. kfree_skb(skb);
  271. return -EINVAL;
  272. }
  273. static int input_action_end_dx4(struct sk_buff *skb,
  274. struct seg6_local_lwt *slwt)
  275. {
  276. struct iphdr *iph;
  277. __be32 nhaddr;
  278. int err;
  279. if (!decap_and_validate(skb, IPPROTO_IPIP))
  280. goto drop;
  281. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  282. goto drop;
  283. skb->protocol = htons(ETH_P_IP);
  284. iph = ip_hdr(skb);
  285. nhaddr = slwt->nh4.s_addr ?: iph->daddr;
  286. skb_dst_drop(skb);
  287. err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
  288. if (err)
  289. goto drop;
  290. return dst_input(skb);
  291. drop:
  292. kfree_skb(skb);
  293. return -EINVAL;
  294. }
  295. static int input_action_end_dt6(struct sk_buff *skb,
  296. struct seg6_local_lwt *slwt)
  297. {
  298. if (!decap_and_validate(skb, IPPROTO_IPV6))
  299. goto drop;
  300. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  301. goto drop;
  302. seg6_lookup_nexthop(skb, NULL, slwt->table);
  303. return dst_input(skb);
  304. drop:
  305. kfree_skb(skb);
  306. return -EINVAL;
  307. }
  308. /* push an SRH on top of the current one */
  309. static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  310. {
  311. struct ipv6_sr_hdr *srh;
  312. int err = -EINVAL;
  313. srh = get_and_validate_srh(skb);
  314. if (!srh)
  315. goto drop;
  316. err = seg6_do_srh_inline(skb, slwt->srh);
  317. if (err)
  318. goto drop;
  319. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  320. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  321. seg6_lookup_nexthop(skb, NULL, 0);
  322. return dst_input(skb);
  323. drop:
  324. kfree_skb(skb);
  325. return err;
  326. }
  327. /* encapsulate within an outer IPv6 header and a specified SRH */
  328. static int input_action_end_b6_encap(struct sk_buff *skb,
  329. struct seg6_local_lwt *slwt)
  330. {
  331. struct ipv6_sr_hdr *srh;
  332. int err = -EINVAL;
  333. srh = get_and_validate_srh(skb);
  334. if (!srh)
  335. goto drop;
  336. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  337. skb_reset_inner_headers(skb);
  338. skb->encapsulation = 1;
  339. err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
  340. if (err)
  341. goto drop;
  342. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  343. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  344. seg6_lookup_nexthop(skb, NULL, 0);
  345. return dst_input(skb);
  346. drop:
  347. kfree_skb(skb);
  348. return err;
  349. }
  350. DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
  351. bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
  352. {
  353. struct seg6_bpf_srh_state *srh_state =
  354. this_cpu_ptr(&seg6_bpf_srh_states);
  355. struct ipv6_sr_hdr *srh = srh_state->srh;
  356. if (unlikely(srh == NULL))
  357. return false;
  358. if (unlikely(!srh_state->valid)) {
  359. if ((srh_state->hdrlen & 7) != 0)
  360. return false;
  361. srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
  362. if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3))
  363. return false;
  364. srh_state->valid = true;
  365. }
  366. return true;
  367. }
  368. static int input_action_end_bpf(struct sk_buff *skb,
  369. struct seg6_local_lwt *slwt)
  370. {
  371. struct seg6_bpf_srh_state *srh_state =
  372. this_cpu_ptr(&seg6_bpf_srh_states);
  373. struct ipv6_sr_hdr *srh;
  374. int ret;
  375. srh = get_and_validate_srh(skb);
  376. if (!srh) {
  377. kfree_skb(skb);
  378. return -EINVAL;
  379. }
  380. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  381. /* preempt_disable is needed to protect the per-CPU buffer srh_state,
  382. * which is also accessed by the bpf_lwt_seg6_* helpers
  383. */
  384. preempt_disable();
  385. srh_state->srh = srh;
  386. srh_state->hdrlen = srh->hdrlen << 3;
  387. srh_state->valid = true;
  388. rcu_read_lock();
  389. bpf_compute_data_pointers(skb);
  390. ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
  391. rcu_read_unlock();
  392. switch (ret) {
  393. case BPF_OK:
  394. case BPF_REDIRECT:
  395. break;
  396. case BPF_DROP:
  397. goto drop;
  398. default:
  399. pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
  400. goto drop;
  401. }
  402. if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
  403. goto drop;
  404. preempt_enable();
  405. if (ret != BPF_REDIRECT)
  406. seg6_lookup_nexthop(skb, NULL, 0);
  407. return dst_input(skb);
  408. drop:
  409. preempt_enable();
  410. kfree_skb(skb);
  411. return -EINVAL;
  412. }
  413. static struct seg6_action_desc seg6_action_table[] = {
  414. {
  415. .action = SEG6_LOCAL_ACTION_END,
  416. .attrs = 0,
  417. .input = input_action_end,
  418. },
  419. {
  420. .action = SEG6_LOCAL_ACTION_END_X,
  421. .attrs = (1 << SEG6_LOCAL_NH6),
  422. .input = input_action_end_x,
  423. },
  424. {
  425. .action = SEG6_LOCAL_ACTION_END_T,
  426. .attrs = (1 << SEG6_LOCAL_TABLE),
  427. .input = input_action_end_t,
  428. },
  429. {
  430. .action = SEG6_LOCAL_ACTION_END_DX2,
  431. .attrs = (1 << SEG6_LOCAL_OIF),
  432. .input = input_action_end_dx2,
  433. },
  434. {
  435. .action = SEG6_LOCAL_ACTION_END_DX6,
  436. .attrs = (1 << SEG6_LOCAL_NH6),
  437. .input = input_action_end_dx6,
  438. },
  439. {
  440. .action = SEG6_LOCAL_ACTION_END_DX4,
  441. .attrs = (1 << SEG6_LOCAL_NH4),
  442. .input = input_action_end_dx4,
  443. },
  444. {
  445. .action = SEG6_LOCAL_ACTION_END_DT6,
  446. .attrs = (1 << SEG6_LOCAL_TABLE),
  447. .input = input_action_end_dt6,
  448. },
  449. {
  450. .action = SEG6_LOCAL_ACTION_END_B6,
  451. .attrs = (1 << SEG6_LOCAL_SRH),
  452. .input = input_action_end_b6,
  453. },
  454. {
  455. .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
  456. .attrs = (1 << SEG6_LOCAL_SRH),
  457. .input = input_action_end_b6_encap,
  458. .static_headroom = sizeof(struct ipv6hdr),
  459. },
  460. {
  461. .action = SEG6_LOCAL_ACTION_END_BPF,
  462. .attrs = (1 << SEG6_LOCAL_BPF),
  463. .input = input_action_end_bpf,
  464. },
  465. };
  466. static struct seg6_action_desc *__get_action_desc(int action)
  467. {
  468. struct seg6_action_desc *desc;
  469. int i, count;
  470. count = ARRAY_SIZE(seg6_action_table);
  471. for (i = 0; i < count; i++) {
  472. desc = &seg6_action_table[i];
  473. if (desc->action == action)
  474. return desc;
  475. }
  476. return NULL;
  477. }
  478. static int seg6_local_input(struct sk_buff *skb)
  479. {
  480. struct dst_entry *orig_dst = skb_dst(skb);
  481. struct seg6_action_desc *desc;
  482. struct seg6_local_lwt *slwt;
  483. if (skb->protocol != htons(ETH_P_IPV6)) {
  484. kfree_skb(skb);
  485. return -EINVAL;
  486. }
  487. slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
  488. desc = slwt->desc;
  489. return desc->input(skb, slwt);
  490. }
  491. static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
  492. [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
  493. [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
  494. [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
  495. [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
  496. .len = sizeof(struct in_addr) },
  497. [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
  498. .len = sizeof(struct in6_addr) },
  499. [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
  500. [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
  501. [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
  502. };
  503. static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  504. {
  505. struct ipv6_sr_hdr *srh;
  506. int len;
  507. srh = nla_data(attrs[SEG6_LOCAL_SRH]);
  508. len = nla_len(attrs[SEG6_LOCAL_SRH]);
  509. /* SRH must contain at least one segment */
  510. if (len < sizeof(*srh) + sizeof(struct in6_addr))
  511. return -EINVAL;
  512. if (!seg6_validate_srh(srh, len))
  513. return -EINVAL;
  514. slwt->srh = kmemdup(srh, len, GFP_KERNEL);
  515. if (!slwt->srh)
  516. return -ENOMEM;
  517. slwt->headroom += len;
  518. return 0;
  519. }
  520. static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  521. {
  522. struct ipv6_sr_hdr *srh;
  523. struct nlattr *nla;
  524. int len;
  525. srh = slwt->srh;
  526. len = (srh->hdrlen + 1) << 3;
  527. nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
  528. if (!nla)
  529. return -EMSGSIZE;
  530. memcpy(nla_data(nla), srh, len);
  531. return 0;
  532. }
  533. static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  534. {
  535. int len = (a->srh->hdrlen + 1) << 3;
  536. if (len != ((b->srh->hdrlen + 1) << 3))
  537. return 1;
  538. return memcmp(a->srh, b->srh, len);
  539. }
  540. static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  541. {
  542. slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
  543. return 0;
  544. }
  545. static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  546. {
  547. if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
  548. return -EMSGSIZE;
  549. return 0;
  550. }
  551. static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  552. {
  553. if (a->table != b->table)
  554. return 1;
  555. return 0;
  556. }
  557. static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  558. {
  559. memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
  560. sizeof(struct in_addr));
  561. return 0;
  562. }
  563. static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  564. {
  565. struct nlattr *nla;
  566. nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
  567. if (!nla)
  568. return -EMSGSIZE;
  569. memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
  570. return 0;
  571. }
  572. static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  573. {
  574. return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
  575. }
  576. static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  577. {
  578. memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
  579. sizeof(struct in6_addr));
  580. return 0;
  581. }
  582. static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  583. {
  584. struct nlattr *nla;
  585. nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
  586. if (!nla)
  587. return -EMSGSIZE;
  588. memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
  589. return 0;
  590. }
  591. static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  592. {
  593. return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
  594. }
  595. static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  596. {
  597. slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
  598. return 0;
  599. }
  600. static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  601. {
  602. if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
  603. return -EMSGSIZE;
  604. return 0;
  605. }
  606. static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  607. {
  608. if (a->iif != b->iif)
  609. return 1;
  610. return 0;
  611. }
  612. static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  613. {
  614. slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
  615. return 0;
  616. }
  617. static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  618. {
  619. if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
  620. return -EMSGSIZE;
  621. return 0;
  622. }
  623. static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  624. {
  625. if (a->oif != b->oif)
  626. return 1;
  627. return 0;
  628. }
  629. #define MAX_PROG_NAME 256
  630. static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
  631. [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
  632. [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  633. .len = MAX_PROG_NAME },
  634. };
  635. static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  636. {
  637. struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
  638. struct bpf_prog *p;
  639. int ret;
  640. u32 fd;
  641. ret = nla_parse_nested(tb, SEG6_LOCAL_BPF_PROG_MAX,
  642. attrs[SEG6_LOCAL_BPF], bpf_prog_policy, NULL);
  643. if (ret < 0)
  644. return ret;
  645. if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
  646. return -EINVAL;
  647. slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
  648. if (!slwt->bpf.name)
  649. return -ENOMEM;
  650. fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
  651. p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
  652. if (IS_ERR(p)) {
  653. kfree(slwt->bpf.name);
  654. return PTR_ERR(p);
  655. }
  656. slwt->bpf.prog = p;
  657. return 0;
  658. }
  659. static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  660. {
  661. struct nlattr *nest;
  662. if (!slwt->bpf.prog)
  663. return 0;
  664. nest = nla_nest_start(skb, SEG6_LOCAL_BPF);
  665. if (!nest)
  666. return -EMSGSIZE;
  667. if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
  668. return -EMSGSIZE;
  669. if (slwt->bpf.name &&
  670. nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
  671. return -EMSGSIZE;
  672. return nla_nest_end(skb, nest);
  673. }
  674. static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  675. {
  676. if (!a->bpf.name && !b->bpf.name)
  677. return 0;
  678. if (!a->bpf.name || !b->bpf.name)
  679. return 1;
  680. return strcmp(a->bpf.name, b->bpf.name);
  681. }
  682. struct seg6_action_param {
  683. int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
  684. int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  685. int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
  686. };
  687. static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
  688. [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
  689. .put = put_nla_srh,
  690. .cmp = cmp_nla_srh },
  691. [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
  692. .put = put_nla_table,
  693. .cmp = cmp_nla_table },
  694. [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
  695. .put = put_nla_nh4,
  696. .cmp = cmp_nla_nh4 },
  697. [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
  698. .put = put_nla_nh6,
  699. .cmp = cmp_nla_nh6 },
  700. [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
  701. .put = put_nla_iif,
  702. .cmp = cmp_nla_iif },
  703. [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
  704. .put = put_nla_oif,
  705. .cmp = cmp_nla_oif },
  706. [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
  707. .put = put_nla_bpf,
  708. .cmp = cmp_nla_bpf },
  709. };
  710. static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
  711. {
  712. struct seg6_action_param *param;
  713. struct seg6_action_desc *desc;
  714. int i, err;
  715. desc = __get_action_desc(slwt->action);
  716. if (!desc)
  717. return -EINVAL;
  718. if (!desc->input)
  719. return -EOPNOTSUPP;
  720. slwt->desc = desc;
  721. slwt->headroom += desc->static_headroom;
  722. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  723. if (desc->attrs & (1 << i)) {
  724. if (!attrs[i])
  725. return -EINVAL;
  726. param = &seg6_action_params[i];
  727. err = param->parse(attrs, slwt);
  728. if (err < 0)
  729. return err;
  730. }
  731. }
  732. return 0;
  733. }
  734. static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
  735. const void *cfg, struct lwtunnel_state **ts,
  736. struct netlink_ext_ack *extack)
  737. {
  738. struct nlattr *tb[SEG6_LOCAL_MAX + 1];
  739. struct lwtunnel_state *newts;
  740. struct seg6_local_lwt *slwt;
  741. int err;
  742. if (family != AF_INET6)
  743. return -EINVAL;
  744. err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
  745. extack);
  746. if (err < 0)
  747. return err;
  748. if (!tb[SEG6_LOCAL_ACTION])
  749. return -EINVAL;
  750. newts = lwtunnel_state_alloc(sizeof(*slwt));
  751. if (!newts)
  752. return -ENOMEM;
  753. slwt = seg6_local_lwtunnel(newts);
  754. slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
  755. err = parse_nla_action(tb, slwt);
  756. if (err < 0)
  757. goto out_free;
  758. newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
  759. newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
  760. newts->headroom = slwt->headroom;
  761. *ts = newts;
  762. return 0;
  763. out_free:
  764. kfree(slwt->srh);
  765. kfree(newts);
  766. return err;
  767. }
  768. static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
  769. {
  770. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  771. kfree(slwt->srh);
  772. if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
  773. kfree(slwt->bpf.name);
  774. bpf_prog_put(slwt->bpf.prog);
  775. }
  776. return;
  777. }
  778. static int seg6_local_fill_encap(struct sk_buff *skb,
  779. struct lwtunnel_state *lwt)
  780. {
  781. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  782. struct seg6_action_param *param;
  783. int i, err;
  784. if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
  785. return -EMSGSIZE;
  786. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  787. if (slwt->desc->attrs & (1 << i)) {
  788. param = &seg6_action_params[i];
  789. err = param->put(skb, slwt);
  790. if (err < 0)
  791. return err;
  792. }
  793. }
  794. return 0;
  795. }
  796. static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
  797. {
  798. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  799. unsigned long attrs;
  800. int nlsize;
  801. nlsize = nla_total_size(4); /* action */
  802. attrs = slwt->desc->attrs;
  803. if (attrs & (1 << SEG6_LOCAL_SRH))
  804. nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
  805. if (attrs & (1 << SEG6_LOCAL_TABLE))
  806. nlsize += nla_total_size(4);
  807. if (attrs & (1 << SEG6_LOCAL_NH4))
  808. nlsize += nla_total_size(4);
  809. if (attrs & (1 << SEG6_LOCAL_NH6))
  810. nlsize += nla_total_size(16);
  811. if (attrs & (1 << SEG6_LOCAL_IIF))
  812. nlsize += nla_total_size(4);
  813. if (attrs & (1 << SEG6_LOCAL_OIF))
  814. nlsize += nla_total_size(4);
  815. if (attrs & (1 << SEG6_LOCAL_BPF))
  816. nlsize += nla_total_size(sizeof(struct nlattr)) +
  817. nla_total_size(MAX_PROG_NAME) +
  818. nla_total_size(4);
  819. return nlsize;
  820. }
  821. static int seg6_local_cmp_encap(struct lwtunnel_state *a,
  822. struct lwtunnel_state *b)
  823. {
  824. struct seg6_local_lwt *slwt_a, *slwt_b;
  825. struct seg6_action_param *param;
  826. int i;
  827. slwt_a = seg6_local_lwtunnel(a);
  828. slwt_b = seg6_local_lwtunnel(b);
  829. if (slwt_a->action != slwt_b->action)
  830. return 1;
  831. if (slwt_a->desc->attrs != slwt_b->desc->attrs)
  832. return 1;
  833. for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
  834. if (slwt_a->desc->attrs & (1 << i)) {
  835. param = &seg6_action_params[i];
  836. if (param->cmp(slwt_a, slwt_b))
  837. return 1;
  838. }
  839. }
  840. return 0;
  841. }
  842. static const struct lwtunnel_encap_ops seg6_local_ops = {
  843. .build_state = seg6_local_build_state,
  844. .destroy_state = seg6_local_destroy_state,
  845. .input = seg6_local_input,
  846. .fill_encap = seg6_local_fill_encap,
  847. .get_encap_size = seg6_local_get_encap_size,
  848. .cmp_encap = seg6_local_cmp_encap,
  849. .owner = THIS_MODULE,
  850. };
  851. int __init seg6_local_init(void)
  852. {
  853. return lwtunnel_encap_add_ops(&seg6_local_ops,
  854. LWTUNNEL_ENCAP_SEG6_LOCAL);
  855. }
  856. void seg6_local_exit(void)
  857. {
  858. lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
  859. }