seg6_local.c 23 KB

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