seg6_local.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SR-IPv6 implementation
  4. *
  5. * Authors:
  6. * David Lebrun <david.lebrun@uclouvain.be>
  7. * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
  8. */
  9. #include <linux/filter.h>
  10. #include <linux/types.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/net.h>
  13. #include <linux/module.h>
  14. #include <net/ip.h>
  15. #include <net/lwtunnel.h>
  16. #include <net/netevent.h>
  17. #include <net/netns/generic.h>
  18. #include <net/ip6_fib.h>
  19. #include <net/route.h>
  20. #include <net/seg6.h>
  21. #include <linux/seg6.h>
  22. #include <linux/seg6_local.h>
  23. #include <net/addrconf.h>
  24. #include <net/ip6_route.h>
  25. #include <net/dst_cache.h>
  26. #include <net/ip_tunnels.h>
  27. #ifdef CONFIG_IPV6_SEG6_HMAC
  28. #include <net/seg6_hmac.h>
  29. #endif
  30. #include <net/seg6_local.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/bpf.h>
  33. #include <linux/netfilter.h>
  34. #define SEG6_F_ATTR(i) BIT(i)
  35. struct seg6_local_lwt;
  36. /* callbacks used for customizing the creation and destruction of a behavior */
  37. struct seg6_local_lwtunnel_ops {
  38. int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
  39. struct netlink_ext_ack *extack);
  40. void (*destroy_state)(struct seg6_local_lwt *slwt);
  41. };
  42. struct seg6_action_desc {
  43. int action;
  44. unsigned long attrs;
  45. /* The optattrs field is used for specifying all the optional
  46. * attributes supported by a specific behavior.
  47. * It means that if one of these attributes is not provided in the
  48. * netlink message during the behavior creation, no errors will be
  49. * returned to the userspace.
  50. *
  51. * Each attribute can be only of two types (mutually exclusive):
  52. * 1) required or 2) optional.
  53. * Every user MUST obey to this rule! If you set an attribute as
  54. * required the same attribute CANNOT be set as optional and vice
  55. * versa.
  56. */
  57. unsigned long optattrs;
  58. int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  59. int static_headroom;
  60. struct seg6_local_lwtunnel_ops slwt_ops;
  61. };
  62. struct bpf_lwt_prog {
  63. struct bpf_prog *prog;
  64. char *name;
  65. };
  66. /* default length values (expressed in bits) for both Locator-Block and
  67. * Locator-Node Function.
  68. *
  69. * Both SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS *must* be:
  70. * i) greater than 0;
  71. * ii) evenly divisible by 8. In other terms, the lengths of the
  72. * Locator-Block and Locator-Node Function must be byte-aligned (we can
  73. * relax this constraint in the future if really needed).
  74. *
  75. * Moreover, a third condition must hold:
  76. * iii) SEG6_LOCAL_LCBLOCK_DBITS + SEG6_LOCAL_LCNODE_FN_DBITS <= 128.
  77. *
  78. * The correctness of SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS
  79. * values are checked during the kernel compilation. If the compilation stops,
  80. * check the value of these parameters to see if they meet conditions (i), (ii)
  81. * and (iii).
  82. */
  83. #define SEG6_LOCAL_LCBLOCK_DBITS 32
  84. #define SEG6_LOCAL_LCNODE_FN_DBITS 16
  85. /* The following next_csid_chk_{cntr,lcblock,lcblock_fn}_bits macros can be
  86. * used directly to check whether the lengths (in bits) of Locator-Block and
  87. * Locator-Node Function are valid according to (i), (ii), (iii).
  88. */
  89. #define next_csid_chk_cntr_bits(blen, flen) \
  90. ((blen) + (flen) > 128)
  91. #define next_csid_chk_lcblock_bits(blen) \
  92. ({ \
  93. typeof(blen) __tmp = blen; \
  94. (!__tmp || __tmp > 120 || (__tmp & 0x07)); \
  95. })
  96. #define next_csid_chk_lcnode_fn_bits(flen) \
  97. next_csid_chk_lcblock_bits(flen)
  98. /* flag indicating that flavors are set up for a given End* behavior */
  99. #define SEG6_F_LOCAL_FLAVORS SEG6_F_ATTR(SEG6_LOCAL_FLAVORS)
  100. #define SEG6_F_LOCAL_FLV_OP(flvname) BIT(SEG6_LOCAL_FLV_OP_##flvname)
  101. #define SEG6_F_LOCAL_FLV_NEXT_CSID SEG6_F_LOCAL_FLV_OP(NEXT_CSID)
  102. #define SEG6_F_LOCAL_FLV_PSP SEG6_F_LOCAL_FLV_OP(PSP)
  103. /* Supported RFC8986 Flavor operations are reported in this bitmask */
  104. #define SEG6_LOCAL_FLV8986_SUPP_OPS SEG6_F_LOCAL_FLV_PSP
  105. #define SEG6_LOCAL_END_FLV_SUPP_OPS (SEG6_F_LOCAL_FLV_NEXT_CSID | \
  106. SEG6_LOCAL_FLV8986_SUPP_OPS)
  107. #define SEG6_LOCAL_END_X_FLV_SUPP_OPS SEG6_F_LOCAL_FLV_NEXT_CSID
  108. struct seg6_flavors_info {
  109. /* Flavor operations */
  110. __u32 flv_ops;
  111. /* Locator-Block length, expressed in bits */
  112. __u8 lcblock_bits;
  113. /* Locator-Node Function length, expressed in bits*/
  114. __u8 lcnode_func_bits;
  115. };
  116. enum seg6_end_dt_mode {
  117. DT_INVALID_MODE = -EINVAL,
  118. DT_LEGACY_MODE = 0,
  119. DT_VRF_MODE = 1,
  120. };
  121. struct seg6_end_dt_info {
  122. enum seg6_end_dt_mode mode;
  123. struct net *net;
  124. /* VRF device associated to the routing table used by the SRv6
  125. * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
  126. */
  127. int vrf_ifindex;
  128. int vrf_table;
  129. /* tunneled packet family (IPv4 or IPv6).
  130. * Protocol and header length are inferred from family.
  131. */
  132. u16 family;
  133. };
  134. struct pcpu_seg6_local_counters {
  135. u64_stats_t packets;
  136. u64_stats_t bytes;
  137. u64_stats_t errors;
  138. struct u64_stats_sync syncp;
  139. };
  140. /* This struct groups all the SRv6 Behavior counters supported so far.
  141. *
  142. * put_nla_counters() makes use of this data structure to collect all counter
  143. * values after the per-CPU counter evaluation has been performed.
  144. * Finally, each counter value (in seg6_local_counters) is stored in the
  145. * corresponding netlink attribute and sent to user space.
  146. *
  147. * NB: we don't want to expose this structure to user space!
  148. */
  149. struct seg6_local_counters {
  150. __u64 packets;
  151. __u64 bytes;
  152. __u64 errors;
  153. };
  154. #define seg6_local_alloc_pcpu_counters(__gfp) \
  155. __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
  156. ((__gfp) | __GFP_ZERO))
  157. #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
  158. struct seg6_local_lwt {
  159. int action;
  160. struct ipv6_sr_hdr *srh;
  161. int table;
  162. struct in_addr nh4;
  163. struct in6_addr nh6;
  164. int iif;
  165. int oif;
  166. struct bpf_lwt_prog bpf;
  167. #ifdef CONFIG_NET_L3_MASTER_DEV
  168. struct seg6_end_dt_info dt_info;
  169. #endif
  170. struct seg6_flavors_info flv_info;
  171. struct pcpu_seg6_local_counters __percpu *pcpu_counters;
  172. int headroom;
  173. struct seg6_action_desc *desc;
  174. /* unlike the required attrs, we have to track the optional attributes
  175. * that have been effectively parsed.
  176. */
  177. unsigned long parsed_optattrs;
  178. };
  179. static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
  180. {
  181. return (struct seg6_local_lwt *)lwt->data;
  182. }
  183. static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
  184. {
  185. struct ipv6_sr_hdr *srh;
  186. srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
  187. if (!srh)
  188. return NULL;
  189. #ifdef CONFIG_IPV6_SEG6_HMAC
  190. if (!seg6_hmac_validate_skb(skb))
  191. return NULL;
  192. #endif
  193. return srh;
  194. }
  195. static bool decap_and_validate(struct sk_buff *skb, int proto)
  196. {
  197. struct ipv6_sr_hdr *srh;
  198. unsigned int off = 0;
  199. srh = seg6_get_srh(skb, 0);
  200. if (srh && srh->segments_left > 0)
  201. return false;
  202. #ifdef CONFIG_IPV6_SEG6_HMAC
  203. if (srh && !seg6_hmac_validate_skb(skb))
  204. return false;
  205. #endif
  206. if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
  207. return false;
  208. if (!pskb_pull(skb, off))
  209. return false;
  210. skb_postpull_rcsum(skb, skb_network_header(skb), off);
  211. skb_reset_network_header(skb);
  212. skb_reset_transport_header(skb);
  213. if (iptunnel_pull_offloads(skb))
  214. return false;
  215. return true;
  216. }
  217. static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
  218. {
  219. struct in6_addr *addr;
  220. srh->segments_left--;
  221. addr = srh->segments + srh->segments_left;
  222. *daddr = *addr;
  223. }
  224. static int
  225. seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
  226. u32 tbl_id, bool local_delivery)
  227. {
  228. struct net *net = dev_net(skb->dev);
  229. struct ipv6hdr *hdr = ipv6_hdr(skb);
  230. int flags = RT6_LOOKUP_F_HAS_SADDR;
  231. struct dst_entry *dst = NULL;
  232. struct rt6_info *rt;
  233. struct flowi6 fl6;
  234. int dev_flags = 0;
  235. memset(&fl6, 0, sizeof(fl6));
  236. fl6.flowi6_iif = skb->dev->ifindex;
  237. fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
  238. fl6.saddr = hdr->saddr;
  239. fl6.flowlabel = ip6_flowinfo(hdr);
  240. fl6.flowi6_mark = skb->mark;
  241. fl6.flowi6_proto = hdr->nexthdr;
  242. if (nhaddr)
  243. fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
  244. if (!tbl_id) {
  245. dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
  246. } else {
  247. struct fib6_table *table;
  248. table = fib6_get_table(net, tbl_id);
  249. if (!table)
  250. goto out;
  251. rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
  252. dst = &rt->dst;
  253. }
  254. /* we want to discard traffic destined for local packet processing,
  255. * if @local_delivery is set to false.
  256. */
  257. if (!local_delivery)
  258. dev_flags |= IFF_LOOPBACK;
  259. if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
  260. dst_release(dst);
  261. dst = NULL;
  262. }
  263. out:
  264. if (!dst) {
  265. rt = net->ipv6.ip6_blk_hole_entry;
  266. dst = &rt->dst;
  267. dst_hold(dst);
  268. }
  269. skb_dst_drop(skb);
  270. skb_dst_set(skb, dst);
  271. return dst->error;
  272. }
  273. int seg6_lookup_nexthop(struct sk_buff *skb,
  274. struct in6_addr *nhaddr, u32 tbl_id)
  275. {
  276. return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
  277. }
  278. static __u8 seg6_flv_lcblock_octects(const struct seg6_flavors_info *finfo)
  279. {
  280. return finfo->lcblock_bits >> 3;
  281. }
  282. static __u8 seg6_flv_lcnode_func_octects(const struct seg6_flavors_info *finfo)
  283. {
  284. return finfo->lcnode_func_bits >> 3;
  285. }
  286. static bool seg6_next_csid_is_arg_zero(const struct in6_addr *addr,
  287. const struct seg6_flavors_info *finfo)
  288. {
  289. __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
  290. __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
  291. __u8 arg_octects;
  292. int i;
  293. arg_octects = 16 - blk_octects - fnc_octects;
  294. for (i = 0; i < arg_octects; ++i) {
  295. if (addr->s6_addr[blk_octects + fnc_octects + i] != 0x00)
  296. return false;
  297. }
  298. return true;
  299. }
  300. /* assume that DA.Argument length > 0 */
  301. static void seg6_next_csid_advance_arg(struct in6_addr *addr,
  302. const struct seg6_flavors_info *finfo)
  303. {
  304. __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
  305. __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
  306. /* advance DA.Argument */
  307. memmove(&addr->s6_addr[blk_octects],
  308. &addr->s6_addr[blk_octects + fnc_octects],
  309. 16 - blk_octects - fnc_octects);
  310. memset(&addr->s6_addr[16 - fnc_octects], 0x00, fnc_octects);
  311. }
  312. static int input_action_end_finish(struct sk_buff *skb,
  313. struct seg6_local_lwt *slwt)
  314. {
  315. seg6_lookup_nexthop(skb, NULL, 0);
  316. return dst_input(skb);
  317. }
  318. static int input_action_end_core(struct sk_buff *skb,
  319. struct seg6_local_lwt *slwt)
  320. {
  321. struct ipv6_sr_hdr *srh;
  322. srh = get_and_validate_srh(skb);
  323. if (!srh)
  324. goto drop;
  325. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  326. return input_action_end_finish(skb, slwt);
  327. drop:
  328. kfree_skb(skb);
  329. return -EINVAL;
  330. }
  331. static int end_next_csid_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  332. {
  333. const struct seg6_flavors_info *finfo = &slwt->flv_info;
  334. struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  335. if (seg6_next_csid_is_arg_zero(daddr, finfo))
  336. return input_action_end_core(skb, slwt);
  337. /* update DA */
  338. seg6_next_csid_advance_arg(daddr, finfo);
  339. return input_action_end_finish(skb, slwt);
  340. }
  341. static int input_action_end_x_finish(struct sk_buff *skb,
  342. struct seg6_local_lwt *slwt)
  343. {
  344. seg6_lookup_nexthop(skb, &slwt->nh6, 0);
  345. return dst_input(skb);
  346. }
  347. static int input_action_end_x_core(struct sk_buff *skb,
  348. struct seg6_local_lwt *slwt)
  349. {
  350. struct ipv6_sr_hdr *srh;
  351. srh = get_and_validate_srh(skb);
  352. if (!srh)
  353. goto drop;
  354. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  355. return input_action_end_x_finish(skb, slwt);
  356. drop:
  357. kfree_skb(skb);
  358. return -EINVAL;
  359. }
  360. static int end_x_next_csid_core(struct sk_buff *skb,
  361. struct seg6_local_lwt *slwt)
  362. {
  363. const struct seg6_flavors_info *finfo = &slwt->flv_info;
  364. struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  365. if (seg6_next_csid_is_arg_zero(daddr, finfo))
  366. return input_action_end_x_core(skb, slwt);
  367. /* update DA */
  368. seg6_next_csid_advance_arg(daddr, finfo);
  369. return input_action_end_x_finish(skb, slwt);
  370. }
  371. static bool seg6_next_csid_enabled(__u32 fops)
  372. {
  373. return fops & SEG6_F_LOCAL_FLV_NEXT_CSID;
  374. }
  375. /* Processing of SRv6 End, End.X, and End.T behaviors can be extended through
  376. * the flavors framework. These behaviors must report the subset of (flavor)
  377. * operations they currently implement. In this way, if a user specifies a
  378. * flavor combination that is not supported by a given End* behavior, the
  379. * kernel refuses to instantiate the tunnel reporting the error.
  380. */
  381. static int seg6_flv_supp_ops_by_action(int action, __u32 *fops)
  382. {
  383. switch (action) {
  384. case SEG6_LOCAL_ACTION_END:
  385. *fops = SEG6_LOCAL_END_FLV_SUPP_OPS;
  386. break;
  387. case SEG6_LOCAL_ACTION_END_X:
  388. *fops = SEG6_LOCAL_END_X_FLV_SUPP_OPS;
  389. break;
  390. default:
  391. return -EOPNOTSUPP;
  392. }
  393. return 0;
  394. }
  395. /* We describe the packet state in relation to the absence/presence of the SRH
  396. * and the Segment Left (SL) field.
  397. * For our purposes, it is not necessary to record the exact value of the SL
  398. * when the SID List consists of two or more segments.
  399. */
  400. enum seg6_local_pktinfo {
  401. /* the order really matters! */
  402. SEG6_LOCAL_PKTINFO_NOHDR = 0,
  403. SEG6_LOCAL_PKTINFO_SL_ZERO,
  404. SEG6_LOCAL_PKTINFO_SL_ONE,
  405. SEG6_LOCAL_PKTINFO_SL_MORE,
  406. __SEG6_LOCAL_PKTINFO_MAX,
  407. };
  408. #define SEG6_LOCAL_PKTINFO_MAX (__SEG6_LOCAL_PKTINFO_MAX - 1)
  409. static enum seg6_local_pktinfo seg6_get_srh_pktinfo(struct ipv6_sr_hdr *srh)
  410. {
  411. __u8 sgl;
  412. if (!srh)
  413. return SEG6_LOCAL_PKTINFO_NOHDR;
  414. sgl = srh->segments_left;
  415. if (sgl < 2)
  416. return SEG6_LOCAL_PKTINFO_SL_ZERO + sgl;
  417. return SEG6_LOCAL_PKTINFO_SL_MORE;
  418. }
  419. enum seg6_local_flv_action {
  420. SEG6_LOCAL_FLV_ACT_UNSPEC = 0,
  421. SEG6_LOCAL_FLV_ACT_END,
  422. SEG6_LOCAL_FLV_ACT_PSP,
  423. SEG6_LOCAL_FLV_ACT_USP,
  424. SEG6_LOCAL_FLV_ACT_USD,
  425. __SEG6_LOCAL_FLV_ACT_MAX
  426. };
  427. #define SEG6_LOCAL_FLV_ACT_MAX (__SEG6_LOCAL_FLV_ACT_MAX - 1)
  428. /* The action table for RFC8986 flavors (see the flv8986_act_tbl below)
  429. * contains the actions (i.e. processing operations) to be applied on packets
  430. * when flavors are configured for an End* behavior.
  431. * By combining the pkinfo data and from the flavors mask, the macro
  432. * computes the index used to access the elements (actions) stored in the
  433. * action table. The index is structured as follows:
  434. *
  435. * index
  436. * _______________/\________________
  437. * / \
  438. * +----------------+----------------+
  439. * | pf | afm |
  440. * +----------------+----------------+
  441. * ph-1 ... p1 p0 fk-1 ... f1 f0
  442. * MSB LSB
  443. *
  444. * where:
  445. * - 'afm' (adjusted flavor mask) is the mask containing a combination of the
  446. * RFC8986 flavors currently supported. 'afm' corresponds to the @fm
  447. * argument of the macro whose value is righ-shifted by 1 bit. By doing so,
  448. * we discard the SEG6_LOCAL_FLV_OP_UNSPEC flag (bit 0 in @fm) which is
  449. * never used here;
  450. * - 'pf' encodes the packet info (pktinfo) regarding the presence/absence of
  451. * the SRH, SL = 0, etc. 'pf' is set with the value of @pf provided as
  452. * argument to the macro.
  453. */
  454. #define flv8986_act_tbl_idx(pf, fm) \
  455. ((((pf) << bits_per(SEG6_LOCAL_FLV8986_SUPP_OPS)) | \
  456. ((fm) & SEG6_LOCAL_FLV8986_SUPP_OPS)) >> SEG6_LOCAL_FLV_OP_PSP)
  457. /* We compute the size of the action table by considering the RFC8986 flavors
  458. * actually supported by the kernel. In this way, the size is automatically
  459. * adjusted when new flavors are supported.
  460. */
  461. #define FLV8986_ACT_TBL_SIZE \
  462. roundup_pow_of_two(flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_MAX, \
  463. SEG6_LOCAL_FLV8986_SUPP_OPS))
  464. /* tbl_cfg(act, pf, fm) macro is used to easily configure the action
  465. * table; it accepts 3 arguments:
  466. * i) @act, the suffix from SEG6_LOCAL_FLV_ACT_{act} representing
  467. * the action that should be applied on the packet;
  468. * ii) @pf, the suffix from SEG6_LOCAL_PKTINFO_{pf} reporting the packet
  469. * info about the lack/presence of SRH, SRH with SL = 0, etc;
  470. * iii) @fm, the mask of flavors.
  471. */
  472. #define tbl_cfg(act, pf, fm) \
  473. [flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_##pf, \
  474. (fm))] = SEG6_LOCAL_FLV_ACT_##act
  475. /* shorthand for improving readability */
  476. #define F_PSP SEG6_F_LOCAL_FLV_PSP
  477. /* The table contains, for each combination of the pktinfo data and
  478. * flavors, the action that should be taken on a packet (e.g.
  479. * "standard" Endpoint processing, Penultimate Segment Pop, etc).
  480. *
  481. * By default, table entries not explicitly configured are initialized with the
  482. * SEG6_LOCAL_FLV_ACT_UNSPEC action, which generally has the effect of
  483. * discarding the processed packet.
  484. */
  485. static const u8 flv8986_act_tbl[FLV8986_ACT_TBL_SIZE] = {
  486. /* PSP variant for packet where SRH with SL = 1 */
  487. tbl_cfg(PSP, SL_ONE, F_PSP),
  488. /* End for packet where the SRH with SL > 1*/
  489. tbl_cfg(END, SL_MORE, F_PSP),
  490. };
  491. #undef F_PSP
  492. #undef tbl_cfg
  493. /* For each flavor defined in RFC8986 (or a combination of them) an action is
  494. * performed on the packet. The specific action depends on:
  495. * - info extracted from the packet (i.e. pktinfo data) regarding the
  496. * lack/presence of the SRH, and if the SRH is available, on the value of
  497. * Segment Left field;
  498. * - the mask of flavors configured for the specific SRv6 End* behavior.
  499. *
  500. * The function combines both the pkinfo and the flavors mask to evaluate the
  501. * corresponding action to be taken on the packet.
  502. */
  503. static enum seg6_local_flv_action
  504. seg6_local_flv8986_act_lookup(enum seg6_local_pktinfo pinfo, __u32 flvmask)
  505. {
  506. unsigned long index;
  507. /* check if the provided mask of flavors is supported */
  508. if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS))
  509. return SEG6_LOCAL_FLV_ACT_UNSPEC;
  510. index = flv8986_act_tbl_idx(pinfo, flvmask);
  511. if (unlikely(index >= FLV8986_ACT_TBL_SIZE))
  512. return SEG6_LOCAL_FLV_ACT_UNSPEC;
  513. return flv8986_act_tbl[index];
  514. }
  515. /* skb->data must be aligned with skb->network_header */
  516. static bool seg6_pop_srh(struct sk_buff *skb, int srhoff)
  517. {
  518. struct ipv6_sr_hdr *srh;
  519. struct ipv6hdr *iph;
  520. __u8 srh_nexthdr;
  521. int thoff = -1;
  522. int srhlen;
  523. int nhlen;
  524. if (unlikely(srhoff < sizeof(*iph) ||
  525. !pskb_may_pull(skb, srhoff + sizeof(*srh))))
  526. return false;
  527. srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
  528. srhlen = ipv6_optlen(srh);
  529. /* we are about to mangle the pkt, let's check if we can write on it */
  530. if (unlikely(skb_ensure_writable(skb, srhoff + srhlen)))
  531. return false;
  532. /* skb_ensure_writable() may change skb pointers; evaluate srh again */
  533. srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
  534. srh_nexthdr = srh->nexthdr;
  535. if (unlikely(!skb_transport_header_was_set(skb)))
  536. goto pull;
  537. nhlen = skb_network_header_len(skb);
  538. /* we have to deal with the transport header: it could be set before
  539. * the SRH, after the SRH, or within it (which is considered wrong,
  540. * however).
  541. */
  542. if (likely(nhlen <= srhoff))
  543. thoff = nhlen;
  544. else if (nhlen >= srhoff + srhlen)
  545. /* transport_header is set after the SRH */
  546. thoff = nhlen - srhlen;
  547. else
  548. /* transport_header falls inside the SRH; hence, we can't
  549. * restore the transport_header pointer properly after
  550. * SRH removing operation.
  551. */
  552. return false;
  553. pull:
  554. /* we need to pop the SRH:
  555. * 1) first of all, we pull out everything from IPv6 header up to SRH
  556. * (included) evaluating also the rcsum;
  557. * 2) we overwrite (and then remove) the SRH by properly moving the
  558. * IPv6 along with any extension header that precedes the SRH;
  559. * 3) At the end, we push back the pulled headers (except for SRH,
  560. * obviously).
  561. */
  562. skb_pull_rcsum(skb, srhoff + srhlen);
  563. memmove(skb_network_header(skb) + srhlen, skb_network_header(skb),
  564. srhoff);
  565. skb_push(skb, srhoff);
  566. skb_reset_network_header(skb);
  567. skb_mac_header_rebuild(skb);
  568. if (likely(thoff >= 0))
  569. skb_set_transport_header(skb, thoff);
  570. iph = ipv6_hdr(skb);
  571. if (iph->nexthdr == NEXTHDR_ROUTING) {
  572. iph->nexthdr = srh_nexthdr;
  573. } else {
  574. /* we must look for the extension header (EXTH, for short) that
  575. * immediately precedes the SRH we have just removed.
  576. * Then, we update the value of the EXTH nexthdr with the one
  577. * contained in the SRH nexthdr.
  578. */
  579. unsigned int off = sizeof(*iph);
  580. struct ipv6_opt_hdr *hp, _hdr;
  581. __u8 nexthdr = iph->nexthdr;
  582. for (;;) {
  583. if (unlikely(!ipv6_ext_hdr(nexthdr) ||
  584. nexthdr == NEXTHDR_NONE))
  585. return false;
  586. hp = skb_header_pointer(skb, off, sizeof(_hdr), &_hdr);
  587. if (unlikely(!hp))
  588. return false;
  589. if (hp->nexthdr == NEXTHDR_ROUTING) {
  590. hp->nexthdr = srh_nexthdr;
  591. break;
  592. }
  593. switch (nexthdr) {
  594. case NEXTHDR_FRAGMENT:
  595. fallthrough;
  596. case NEXTHDR_AUTH:
  597. /* we expect SRH before FRAG and AUTH */
  598. return false;
  599. default:
  600. off += ipv6_optlen(hp);
  601. break;
  602. }
  603. nexthdr = hp->nexthdr;
  604. }
  605. }
  606. iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  607. skb_postpush_rcsum(skb, iph, srhoff);
  608. return true;
  609. }
  610. /* process the packet on the basis of the RFC8986 flavors set for the given
  611. * SRv6 End behavior instance.
  612. */
  613. static int end_flv8986_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  614. {
  615. const struct seg6_flavors_info *finfo = &slwt->flv_info;
  616. enum seg6_local_flv_action action;
  617. enum seg6_local_pktinfo pinfo;
  618. struct ipv6_sr_hdr *srh;
  619. __u32 flvmask;
  620. int srhoff;
  621. srh = seg6_get_srh(skb, 0);
  622. srhoff = srh ? ((unsigned char *)srh - skb->data) : 0;
  623. pinfo = seg6_get_srh_pktinfo(srh);
  624. #ifdef CONFIG_IPV6_SEG6_HMAC
  625. if (srh && !seg6_hmac_validate_skb(skb))
  626. goto drop;
  627. #endif
  628. flvmask = finfo->flv_ops;
  629. if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS)) {
  630. pr_warn_once("seg6local: invalid RFC8986 flavors\n");
  631. goto drop;
  632. }
  633. /* retrieve the action triggered by the combination of pktinfo data and
  634. * the flavors mask.
  635. */
  636. action = seg6_local_flv8986_act_lookup(pinfo, flvmask);
  637. switch (action) {
  638. case SEG6_LOCAL_FLV_ACT_END:
  639. /* process the packet as the "standard" End behavior */
  640. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  641. break;
  642. case SEG6_LOCAL_FLV_ACT_PSP:
  643. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  644. if (unlikely(!seg6_pop_srh(skb, srhoff)))
  645. goto drop;
  646. break;
  647. case SEG6_LOCAL_FLV_ACT_UNSPEC:
  648. fallthrough;
  649. default:
  650. /* by default, we drop the packet since we could not find a
  651. * suitable action.
  652. */
  653. goto drop;
  654. }
  655. return input_action_end_finish(skb, slwt);
  656. drop:
  657. kfree_skb(skb);
  658. return -EINVAL;
  659. }
  660. /* regular endpoint function */
  661. static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  662. {
  663. const struct seg6_flavors_info *finfo = &slwt->flv_info;
  664. __u32 fops = finfo->flv_ops;
  665. if (!fops)
  666. return input_action_end_core(skb, slwt);
  667. /* check for the presence of NEXT-C-SID since it applies first */
  668. if (seg6_next_csid_enabled(fops))
  669. return end_next_csid_core(skb, slwt);
  670. /* the specific processing function to be performed on the packet
  671. * depends on the combination of flavors defined in RFC8986 and some
  672. * information extracted from the packet, e.g. presence/absence of SRH,
  673. * Segment Left = 0, etc.
  674. */
  675. return end_flv8986_core(skb, slwt);
  676. }
  677. /* regular endpoint, and forward to specified nexthop */
  678. static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  679. {
  680. const struct seg6_flavors_info *finfo = &slwt->flv_info;
  681. __u32 fops = finfo->flv_ops;
  682. /* check for the presence of NEXT-C-SID since it applies first */
  683. if (seg6_next_csid_enabled(fops))
  684. return end_x_next_csid_core(skb, slwt);
  685. return input_action_end_x_core(skb, slwt);
  686. }
  687. static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  688. {
  689. struct ipv6_sr_hdr *srh;
  690. srh = get_and_validate_srh(skb);
  691. if (!srh)
  692. goto drop;
  693. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  694. seg6_lookup_nexthop(skb, NULL, slwt->table);
  695. return dst_input(skb);
  696. drop:
  697. kfree_skb(skb);
  698. return -EINVAL;
  699. }
  700. /* decapsulate and forward inner L2 frame on specified interface */
  701. static int input_action_end_dx2(struct sk_buff *skb,
  702. struct seg6_local_lwt *slwt)
  703. {
  704. struct net *net = dev_net(skb->dev);
  705. struct net_device *odev;
  706. struct ethhdr *eth;
  707. if (!decap_and_validate(skb, IPPROTO_ETHERNET))
  708. goto drop;
  709. if (!pskb_may_pull(skb, ETH_HLEN))
  710. goto drop;
  711. skb_reset_mac_header(skb);
  712. eth = (struct ethhdr *)skb->data;
  713. /* To determine the frame's protocol, we assume it is 802.3. This avoids
  714. * a call to eth_type_trans(), which is not really relevant for our
  715. * use case.
  716. */
  717. if (!eth_proto_is_802_3(eth->h_proto))
  718. goto drop;
  719. odev = dev_get_by_index_rcu(net, slwt->oif);
  720. if (!odev)
  721. goto drop;
  722. /* As we accept Ethernet frames, make sure the egress device is of
  723. * the correct type.
  724. */
  725. if (odev->type != ARPHRD_ETHER)
  726. goto drop;
  727. if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
  728. goto drop;
  729. skb_orphan(skb);
  730. if (skb_warn_if_lro(skb))
  731. goto drop;
  732. skb_forward_csum(skb);
  733. if (skb->len - ETH_HLEN > odev->mtu)
  734. goto drop;
  735. skb->dev = odev;
  736. skb->protocol = eth->h_proto;
  737. return dev_queue_xmit(skb);
  738. drop:
  739. kfree_skb(skb);
  740. return -EINVAL;
  741. }
  742. static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
  743. struct sk_buff *skb)
  744. {
  745. struct dst_entry *orig_dst = skb_dst(skb);
  746. struct in6_addr *nhaddr = NULL;
  747. struct seg6_local_lwt *slwt;
  748. slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
  749. /* The inner packet is not associated to any local interface,
  750. * so we do not call netif_rx().
  751. *
  752. * If slwt->nh6 is set to ::, then lookup the nexthop for the
  753. * inner packet's DA. Otherwise, use the specified nexthop.
  754. */
  755. if (!ipv6_addr_any(&slwt->nh6))
  756. nhaddr = &slwt->nh6;
  757. seg6_lookup_nexthop(skb, nhaddr, 0);
  758. return dst_input(skb);
  759. }
  760. /* decapsulate and forward to specified nexthop */
  761. static int input_action_end_dx6(struct sk_buff *skb,
  762. struct seg6_local_lwt *slwt)
  763. {
  764. /* this function accepts IPv6 encapsulated packets, with either
  765. * an SRH with SL=0, or no SRH.
  766. */
  767. if (!decap_and_validate(skb, IPPROTO_IPV6))
  768. goto drop;
  769. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  770. goto drop;
  771. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  772. nf_reset_ct(skb);
  773. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  774. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  775. dev_net(skb->dev), NULL, skb, skb->dev,
  776. NULL, input_action_end_dx6_finish);
  777. return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
  778. drop:
  779. kfree_skb(skb);
  780. return -EINVAL;
  781. }
  782. static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
  783. struct sk_buff *skb)
  784. {
  785. struct dst_entry *orig_dst = skb_dst(skb);
  786. struct seg6_local_lwt *slwt;
  787. struct iphdr *iph;
  788. __be32 nhaddr;
  789. int err;
  790. slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
  791. iph = ip_hdr(skb);
  792. nhaddr = slwt->nh4.s_addr ?: iph->daddr;
  793. skb_dst_drop(skb);
  794. err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
  795. if (err) {
  796. kfree_skb(skb);
  797. return -EINVAL;
  798. }
  799. return dst_input(skb);
  800. }
  801. static int input_action_end_dx4(struct sk_buff *skb,
  802. struct seg6_local_lwt *slwt)
  803. {
  804. if (!decap_and_validate(skb, IPPROTO_IPIP))
  805. goto drop;
  806. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  807. goto drop;
  808. skb->protocol = htons(ETH_P_IP);
  809. skb_set_transport_header(skb, sizeof(struct iphdr));
  810. nf_reset_ct(skb);
  811. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  812. return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
  813. dev_net(skb->dev), NULL, skb, skb->dev,
  814. NULL, input_action_end_dx4_finish);
  815. return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
  816. drop:
  817. kfree_skb(skb);
  818. return -EINVAL;
  819. }
  820. #ifdef CONFIG_NET_L3_MASTER_DEV
  821. static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
  822. {
  823. const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
  824. return nli->nl_net;
  825. }
  826. static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
  827. u16 family, struct netlink_ext_ack *extack)
  828. {
  829. struct seg6_end_dt_info *info = &slwt->dt_info;
  830. int vrf_ifindex;
  831. struct net *net;
  832. net = fib6_config_get_net(cfg);
  833. /* note that vrf_table was already set by parse_nla_vrftable() */
  834. vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
  835. info->vrf_table);
  836. if (vrf_ifindex < 0) {
  837. if (vrf_ifindex == -EPERM) {
  838. NL_SET_ERR_MSG(extack,
  839. "Strict mode for VRF is disabled");
  840. } else if (vrf_ifindex == -ENODEV) {
  841. NL_SET_ERR_MSG(extack,
  842. "Table has no associated VRF device");
  843. } else {
  844. pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
  845. vrf_ifindex);
  846. }
  847. return vrf_ifindex;
  848. }
  849. info->net = net;
  850. info->vrf_ifindex = vrf_ifindex;
  851. info->family = family;
  852. info->mode = DT_VRF_MODE;
  853. return 0;
  854. }
  855. /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
  856. * routes the IPv4/IPv6 packet by looking at the configured routing table.
  857. *
  858. * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
  859. * Routing Header packets) from several interfaces and the outer IPv6
  860. * destination address (DA) is used for retrieving the specific instance of the
  861. * End.DT4/DT6 behavior that should process the packets.
  862. *
  863. * However, the inner IPv4/IPv6 packet is not really bound to any receiving
  864. * interface and thus the End.DT4/DT6 sets the VRF (associated with the
  865. * corresponding routing table) as the *receiving* interface.
  866. * In other words, the End.DT4/DT6 processes a packet as if it has been received
  867. * directly by the VRF (and not by one of its slave devices, if any).
  868. * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
  869. * according to the routing table configured by the End.DT4/DT6 instance.
  870. *
  871. * This design allows you to get some interesting features like:
  872. * 1) the statistics on rx packets;
  873. * 2) the possibility to install a packet sniffer on the receiving interface
  874. * (the VRF one) for looking at the incoming packets;
  875. * 3) the possibility to leverage the netfilter prerouting hook for the inner
  876. * IPv4 packet.
  877. *
  878. * This function returns:
  879. * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
  880. * - NULL when the skb is consumed by the VRF rcv handler;
  881. * - a pointer which encodes a negative error number in case of error.
  882. * Note that in this case, the function takes care of freeing the skb.
  883. */
  884. static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
  885. struct net_device *dev)
  886. {
  887. /* based on l3mdev_ip_rcv; we are only interested in the master */
  888. if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
  889. goto drop;
  890. if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
  891. goto drop;
  892. /* the decap packet IPv4/IPv6 does not come with any mac header info.
  893. * We must unset the mac header to allow the VRF device to rebuild it,
  894. * just in case there is a sniffer attached on the device.
  895. */
  896. skb_unset_mac_header(skb);
  897. skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
  898. if (!skb)
  899. /* the skb buffer was consumed by the handler */
  900. return NULL;
  901. /* when a packet is received by a VRF or by one of its slaves, the
  902. * master device reference is set into the skb.
  903. */
  904. if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
  905. goto drop;
  906. return skb;
  907. drop:
  908. kfree_skb(skb);
  909. return ERR_PTR(-EINVAL);
  910. }
  911. static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
  912. struct seg6_end_dt_info *info)
  913. {
  914. int vrf_ifindex = info->vrf_ifindex;
  915. struct net *net = info->net;
  916. if (unlikely(vrf_ifindex < 0))
  917. goto error;
  918. if (unlikely(!net_eq(dev_net(skb->dev), net)))
  919. goto error;
  920. return dev_get_by_index_rcu(net, vrf_ifindex);
  921. error:
  922. return NULL;
  923. }
  924. static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
  925. struct seg6_local_lwt *slwt, u16 family)
  926. {
  927. struct seg6_end_dt_info *info = &slwt->dt_info;
  928. struct net_device *vrf;
  929. __be16 protocol;
  930. int hdrlen;
  931. vrf = end_dt_get_vrf_rcu(skb, info);
  932. if (unlikely(!vrf))
  933. goto drop;
  934. switch (family) {
  935. case AF_INET:
  936. protocol = htons(ETH_P_IP);
  937. hdrlen = sizeof(struct iphdr);
  938. break;
  939. case AF_INET6:
  940. protocol = htons(ETH_P_IPV6);
  941. hdrlen = sizeof(struct ipv6hdr);
  942. break;
  943. case AF_UNSPEC:
  944. fallthrough;
  945. default:
  946. goto drop;
  947. }
  948. if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
  949. pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
  950. goto drop;
  951. }
  952. skb->protocol = protocol;
  953. skb_dst_drop(skb);
  954. skb_set_transport_header(skb, hdrlen);
  955. nf_reset_ct(skb);
  956. return end_dt_vrf_rcv(skb, family, vrf);
  957. drop:
  958. kfree_skb(skb);
  959. return ERR_PTR(-EINVAL);
  960. }
  961. static int input_action_end_dt4(struct sk_buff *skb,
  962. struct seg6_local_lwt *slwt)
  963. {
  964. struct iphdr *iph;
  965. int err;
  966. if (!decap_and_validate(skb, IPPROTO_IPIP))
  967. goto drop;
  968. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  969. goto drop;
  970. skb = end_dt_vrf_core(skb, slwt, AF_INET);
  971. if (!skb)
  972. /* packet has been processed and consumed by the VRF */
  973. return 0;
  974. if (IS_ERR(skb))
  975. return PTR_ERR(skb);
  976. iph = ip_hdr(skb);
  977. err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
  978. if (unlikely(err))
  979. goto drop;
  980. return dst_input(skb);
  981. drop:
  982. kfree_skb(skb);
  983. return -EINVAL;
  984. }
  985. static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
  986. struct netlink_ext_ack *extack)
  987. {
  988. return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
  989. }
  990. static enum
  991. seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
  992. {
  993. unsigned long parsed_optattrs = slwt->parsed_optattrs;
  994. bool legacy, vrfmode;
  995. legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
  996. vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
  997. if (!(legacy ^ vrfmode))
  998. /* both are absent or present: invalid DT6 mode */
  999. return DT_INVALID_MODE;
  1000. return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
  1001. }
  1002. static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
  1003. {
  1004. struct seg6_end_dt_info *info = &slwt->dt_info;
  1005. return info->mode;
  1006. }
  1007. static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
  1008. struct netlink_ext_ack *extack)
  1009. {
  1010. enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
  1011. struct seg6_end_dt_info *info = &slwt->dt_info;
  1012. switch (mode) {
  1013. case DT_LEGACY_MODE:
  1014. info->mode = DT_LEGACY_MODE;
  1015. return 0;
  1016. case DT_VRF_MODE:
  1017. return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
  1018. default:
  1019. NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
  1020. return -EINVAL;
  1021. }
  1022. }
  1023. #endif
  1024. static int input_action_end_dt6(struct sk_buff *skb,
  1025. struct seg6_local_lwt *slwt)
  1026. {
  1027. if (!decap_and_validate(skb, IPPROTO_IPV6))
  1028. goto drop;
  1029. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1030. goto drop;
  1031. #ifdef CONFIG_NET_L3_MASTER_DEV
  1032. if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
  1033. goto legacy_mode;
  1034. /* DT6_VRF_MODE */
  1035. skb = end_dt_vrf_core(skb, slwt, AF_INET6);
  1036. if (!skb)
  1037. /* packet has been processed and consumed by the VRF */
  1038. return 0;
  1039. if (IS_ERR(skb))
  1040. return PTR_ERR(skb);
  1041. /* note: this time we do not need to specify the table because the VRF
  1042. * takes care of selecting the correct table.
  1043. */
  1044. seg6_lookup_any_nexthop(skb, NULL, 0, true);
  1045. return dst_input(skb);
  1046. legacy_mode:
  1047. #endif
  1048. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  1049. seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
  1050. return dst_input(skb);
  1051. drop:
  1052. kfree_skb(skb);
  1053. return -EINVAL;
  1054. }
  1055. #ifdef CONFIG_NET_L3_MASTER_DEV
  1056. static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
  1057. struct netlink_ext_ack *extack)
  1058. {
  1059. return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
  1060. }
  1061. static int input_action_end_dt46(struct sk_buff *skb,
  1062. struct seg6_local_lwt *slwt)
  1063. {
  1064. unsigned int off = 0;
  1065. int nexthdr;
  1066. nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
  1067. if (unlikely(nexthdr < 0))
  1068. goto drop;
  1069. switch (nexthdr) {
  1070. case IPPROTO_IPIP:
  1071. return input_action_end_dt4(skb, slwt);
  1072. case IPPROTO_IPV6:
  1073. return input_action_end_dt6(skb, slwt);
  1074. }
  1075. drop:
  1076. kfree_skb(skb);
  1077. return -EINVAL;
  1078. }
  1079. #endif
  1080. /* push an SRH on top of the current one */
  1081. static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1082. {
  1083. struct ipv6_sr_hdr *srh;
  1084. int err = -EINVAL;
  1085. srh = get_and_validate_srh(skb);
  1086. if (!srh)
  1087. goto drop;
  1088. err = seg6_do_srh_inline(skb, slwt->srh);
  1089. if (err)
  1090. goto drop;
  1091. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  1092. seg6_lookup_nexthop(skb, NULL, 0);
  1093. return dst_input(skb);
  1094. drop:
  1095. kfree_skb(skb);
  1096. return err;
  1097. }
  1098. /* encapsulate within an outer IPv6 header and a specified SRH */
  1099. static int input_action_end_b6_encap(struct sk_buff *skb,
  1100. struct seg6_local_lwt *slwt)
  1101. {
  1102. struct ipv6_sr_hdr *srh;
  1103. int err = -EINVAL;
  1104. srh = get_and_validate_srh(skb);
  1105. if (!srh)
  1106. goto drop;
  1107. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  1108. skb_reset_inner_headers(skb);
  1109. skb->encapsulation = 1;
  1110. err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
  1111. if (err)
  1112. goto drop;
  1113. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  1114. seg6_lookup_nexthop(skb, NULL, 0);
  1115. return dst_input(skb);
  1116. drop:
  1117. kfree_skb(skb);
  1118. return err;
  1119. }
  1120. DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states) = {
  1121. .bh_lock = INIT_LOCAL_LOCK(bh_lock),
  1122. };
  1123. bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
  1124. {
  1125. struct seg6_bpf_srh_state *srh_state =
  1126. this_cpu_ptr(&seg6_bpf_srh_states);
  1127. struct ipv6_sr_hdr *srh = srh_state->srh;
  1128. lockdep_assert_held(&srh_state->bh_lock);
  1129. if (unlikely(srh == NULL))
  1130. return false;
  1131. if (unlikely(!srh_state->valid)) {
  1132. if ((srh_state->hdrlen & 7) != 0)
  1133. return false;
  1134. srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
  1135. if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
  1136. return false;
  1137. srh_state->valid = true;
  1138. }
  1139. return true;
  1140. }
  1141. static int input_action_end_bpf(struct sk_buff *skb,
  1142. struct seg6_local_lwt *slwt)
  1143. {
  1144. struct seg6_bpf_srh_state *srh_state;
  1145. struct ipv6_sr_hdr *srh;
  1146. int ret;
  1147. srh = get_and_validate_srh(skb);
  1148. if (!srh) {
  1149. kfree_skb(skb);
  1150. return -EINVAL;
  1151. }
  1152. advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
  1153. /* The access to the per-CPU buffer srh_state is protected by running
  1154. * always in softirq context (with disabled BH). On PREEMPT_RT the
  1155. * required locking is provided by the following local_lock_nested_bh()
  1156. * statement. It is also accessed by the bpf_lwt_seg6_* helpers via
  1157. * bpf_prog_run_save_cb().
  1158. */
  1159. local_lock_nested_bh(&seg6_bpf_srh_states.bh_lock);
  1160. srh_state = this_cpu_ptr(&seg6_bpf_srh_states);
  1161. srh_state->srh = srh;
  1162. srh_state->hdrlen = srh->hdrlen << 3;
  1163. srh_state->valid = true;
  1164. rcu_read_lock();
  1165. bpf_compute_data_pointers(skb);
  1166. ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
  1167. rcu_read_unlock();
  1168. switch (ret) {
  1169. case BPF_OK:
  1170. case BPF_REDIRECT:
  1171. break;
  1172. case BPF_DROP:
  1173. goto drop;
  1174. default:
  1175. pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
  1176. goto drop;
  1177. }
  1178. if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
  1179. goto drop;
  1180. local_unlock_nested_bh(&seg6_bpf_srh_states.bh_lock);
  1181. if (ret != BPF_REDIRECT)
  1182. seg6_lookup_nexthop(skb, NULL, 0);
  1183. return dst_input(skb);
  1184. drop:
  1185. local_unlock_nested_bh(&seg6_bpf_srh_states.bh_lock);
  1186. kfree_skb(skb);
  1187. return -EINVAL;
  1188. }
  1189. static struct seg6_action_desc seg6_action_table[] = {
  1190. {
  1191. .action = SEG6_LOCAL_ACTION_END,
  1192. .attrs = 0,
  1193. .optattrs = SEG6_F_LOCAL_COUNTERS |
  1194. SEG6_F_LOCAL_FLAVORS,
  1195. .input = input_action_end,
  1196. },
  1197. {
  1198. .action = SEG6_LOCAL_ACTION_END_X,
  1199. .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
  1200. .optattrs = SEG6_F_LOCAL_COUNTERS |
  1201. SEG6_F_LOCAL_FLAVORS,
  1202. .input = input_action_end_x,
  1203. },
  1204. {
  1205. .action = SEG6_LOCAL_ACTION_END_T,
  1206. .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
  1207. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1208. .input = input_action_end_t,
  1209. },
  1210. {
  1211. .action = SEG6_LOCAL_ACTION_END_DX2,
  1212. .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
  1213. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1214. .input = input_action_end_dx2,
  1215. },
  1216. {
  1217. .action = SEG6_LOCAL_ACTION_END_DX6,
  1218. .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
  1219. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1220. .input = input_action_end_dx6,
  1221. },
  1222. {
  1223. .action = SEG6_LOCAL_ACTION_END_DX4,
  1224. .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
  1225. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1226. .input = input_action_end_dx4,
  1227. },
  1228. {
  1229. .action = SEG6_LOCAL_ACTION_END_DT4,
  1230. .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
  1231. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1232. #ifdef CONFIG_NET_L3_MASTER_DEV
  1233. .input = input_action_end_dt4,
  1234. .slwt_ops = {
  1235. .build_state = seg6_end_dt4_build,
  1236. },
  1237. #endif
  1238. },
  1239. {
  1240. .action = SEG6_LOCAL_ACTION_END_DT6,
  1241. #ifdef CONFIG_NET_L3_MASTER_DEV
  1242. .attrs = 0,
  1243. .optattrs = SEG6_F_LOCAL_COUNTERS |
  1244. SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
  1245. SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
  1246. .slwt_ops = {
  1247. .build_state = seg6_end_dt6_build,
  1248. },
  1249. #else
  1250. .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
  1251. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1252. #endif
  1253. .input = input_action_end_dt6,
  1254. },
  1255. {
  1256. .action = SEG6_LOCAL_ACTION_END_DT46,
  1257. .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
  1258. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1259. #ifdef CONFIG_NET_L3_MASTER_DEV
  1260. .input = input_action_end_dt46,
  1261. .slwt_ops = {
  1262. .build_state = seg6_end_dt46_build,
  1263. },
  1264. #endif
  1265. },
  1266. {
  1267. .action = SEG6_LOCAL_ACTION_END_B6,
  1268. .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
  1269. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1270. .input = input_action_end_b6,
  1271. },
  1272. {
  1273. .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
  1274. .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
  1275. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1276. .input = input_action_end_b6_encap,
  1277. .static_headroom = sizeof(struct ipv6hdr),
  1278. },
  1279. {
  1280. .action = SEG6_LOCAL_ACTION_END_BPF,
  1281. .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
  1282. .optattrs = SEG6_F_LOCAL_COUNTERS,
  1283. .input = input_action_end_bpf,
  1284. },
  1285. };
  1286. static struct seg6_action_desc *__get_action_desc(int action)
  1287. {
  1288. struct seg6_action_desc *desc;
  1289. int i, count;
  1290. count = ARRAY_SIZE(seg6_action_table);
  1291. for (i = 0; i < count; i++) {
  1292. desc = &seg6_action_table[i];
  1293. if (desc->action == action)
  1294. return desc;
  1295. }
  1296. return NULL;
  1297. }
  1298. static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
  1299. {
  1300. return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
  1301. }
  1302. static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
  1303. unsigned int len, int err)
  1304. {
  1305. struct pcpu_seg6_local_counters *pcounters;
  1306. pcounters = this_cpu_ptr(slwt->pcpu_counters);
  1307. u64_stats_update_begin(&pcounters->syncp);
  1308. if (likely(!err)) {
  1309. u64_stats_inc(&pcounters->packets);
  1310. u64_stats_add(&pcounters->bytes, len);
  1311. } else {
  1312. u64_stats_inc(&pcounters->errors);
  1313. }
  1314. u64_stats_update_end(&pcounters->syncp);
  1315. }
  1316. static int seg6_local_input_core(struct net *net, struct sock *sk,
  1317. struct sk_buff *skb)
  1318. {
  1319. struct dst_entry *orig_dst = skb_dst(skb);
  1320. struct seg6_action_desc *desc;
  1321. struct seg6_local_lwt *slwt;
  1322. unsigned int len = skb->len;
  1323. int rc;
  1324. slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
  1325. desc = slwt->desc;
  1326. rc = desc->input(skb, slwt);
  1327. if (!seg6_lwtunnel_counters_enabled(slwt))
  1328. return rc;
  1329. seg6_local_update_counters(slwt, len, rc);
  1330. return rc;
  1331. }
  1332. static int seg6_local_input(struct sk_buff *skb)
  1333. {
  1334. if (skb->protocol != htons(ETH_P_IPV6)) {
  1335. kfree_skb(skb);
  1336. return -EINVAL;
  1337. }
  1338. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  1339. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
  1340. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  1341. seg6_local_input_core);
  1342. return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
  1343. }
  1344. static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
  1345. [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
  1346. [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
  1347. [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
  1348. [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
  1349. [SEG6_LOCAL_NH4] = NLA_POLICY_EXACT_LEN(sizeof(struct in_addr)),
  1350. [SEG6_LOCAL_NH6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
  1351. [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
  1352. [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
  1353. [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
  1354. [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
  1355. [SEG6_LOCAL_FLAVORS] = { .type = NLA_NESTED },
  1356. };
  1357. static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1358. struct netlink_ext_ack *extack)
  1359. {
  1360. struct ipv6_sr_hdr *srh;
  1361. int len;
  1362. srh = nla_data(attrs[SEG6_LOCAL_SRH]);
  1363. len = nla_len(attrs[SEG6_LOCAL_SRH]);
  1364. /* SRH must contain at least one segment */
  1365. if (len < sizeof(*srh) + sizeof(struct in6_addr))
  1366. return -EINVAL;
  1367. if (!seg6_validate_srh(srh, len, false))
  1368. return -EINVAL;
  1369. slwt->srh = kmemdup(srh, len, GFP_KERNEL);
  1370. if (!slwt->srh)
  1371. return -ENOMEM;
  1372. slwt->headroom += len;
  1373. return 0;
  1374. }
  1375. static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1376. {
  1377. struct ipv6_sr_hdr *srh;
  1378. struct nlattr *nla;
  1379. int len;
  1380. srh = slwt->srh;
  1381. len = (srh->hdrlen + 1) << 3;
  1382. nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
  1383. if (!nla)
  1384. return -EMSGSIZE;
  1385. memcpy(nla_data(nla), srh, len);
  1386. return 0;
  1387. }
  1388. static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1389. {
  1390. int len = (a->srh->hdrlen + 1) << 3;
  1391. if (len != ((b->srh->hdrlen + 1) << 3))
  1392. return 1;
  1393. return memcmp(a->srh, b->srh, len);
  1394. }
  1395. static void destroy_attr_srh(struct seg6_local_lwt *slwt)
  1396. {
  1397. kfree(slwt->srh);
  1398. }
  1399. static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1400. struct netlink_ext_ack *extack)
  1401. {
  1402. slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
  1403. return 0;
  1404. }
  1405. static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1406. {
  1407. if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
  1408. return -EMSGSIZE;
  1409. return 0;
  1410. }
  1411. static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1412. {
  1413. if (a->table != b->table)
  1414. return 1;
  1415. return 0;
  1416. }
  1417. static struct
  1418. seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
  1419. {
  1420. #ifdef CONFIG_NET_L3_MASTER_DEV
  1421. return &slwt->dt_info;
  1422. #else
  1423. return ERR_PTR(-EOPNOTSUPP);
  1424. #endif
  1425. }
  1426. static int parse_nla_vrftable(struct nlattr **attrs,
  1427. struct seg6_local_lwt *slwt,
  1428. struct netlink_ext_ack *extack)
  1429. {
  1430. struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
  1431. if (IS_ERR(info))
  1432. return PTR_ERR(info);
  1433. info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
  1434. return 0;
  1435. }
  1436. static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1437. {
  1438. struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
  1439. if (IS_ERR(info))
  1440. return PTR_ERR(info);
  1441. if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
  1442. return -EMSGSIZE;
  1443. return 0;
  1444. }
  1445. static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1446. {
  1447. struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
  1448. struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
  1449. if (info_a->vrf_table != info_b->vrf_table)
  1450. return 1;
  1451. return 0;
  1452. }
  1453. static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1454. struct netlink_ext_ack *extack)
  1455. {
  1456. memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
  1457. sizeof(struct in_addr));
  1458. return 0;
  1459. }
  1460. static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1461. {
  1462. struct nlattr *nla;
  1463. nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
  1464. if (!nla)
  1465. return -EMSGSIZE;
  1466. memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
  1467. return 0;
  1468. }
  1469. static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1470. {
  1471. return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
  1472. }
  1473. static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1474. struct netlink_ext_ack *extack)
  1475. {
  1476. memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
  1477. sizeof(struct in6_addr));
  1478. return 0;
  1479. }
  1480. static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1481. {
  1482. struct nlattr *nla;
  1483. nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
  1484. if (!nla)
  1485. return -EMSGSIZE;
  1486. memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
  1487. return 0;
  1488. }
  1489. static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1490. {
  1491. return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
  1492. }
  1493. static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1494. struct netlink_ext_ack *extack)
  1495. {
  1496. slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
  1497. return 0;
  1498. }
  1499. static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1500. {
  1501. if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
  1502. return -EMSGSIZE;
  1503. return 0;
  1504. }
  1505. static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1506. {
  1507. if (a->iif != b->iif)
  1508. return 1;
  1509. return 0;
  1510. }
  1511. static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1512. struct netlink_ext_ack *extack)
  1513. {
  1514. slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
  1515. return 0;
  1516. }
  1517. static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1518. {
  1519. if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
  1520. return -EMSGSIZE;
  1521. return 0;
  1522. }
  1523. static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1524. {
  1525. if (a->oif != b->oif)
  1526. return 1;
  1527. return 0;
  1528. }
  1529. #define MAX_PROG_NAME 256
  1530. static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
  1531. [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
  1532. [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  1533. .len = MAX_PROG_NAME },
  1534. };
  1535. static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1536. struct netlink_ext_ack *extack)
  1537. {
  1538. struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
  1539. struct bpf_prog *p;
  1540. int ret;
  1541. u32 fd;
  1542. ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
  1543. attrs[SEG6_LOCAL_BPF],
  1544. bpf_prog_policy, NULL);
  1545. if (ret < 0)
  1546. return ret;
  1547. if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
  1548. return -EINVAL;
  1549. slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
  1550. if (!slwt->bpf.name)
  1551. return -ENOMEM;
  1552. fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
  1553. p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
  1554. if (IS_ERR(p)) {
  1555. kfree(slwt->bpf.name);
  1556. return PTR_ERR(p);
  1557. }
  1558. slwt->bpf.prog = p;
  1559. return 0;
  1560. }
  1561. static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1562. {
  1563. struct nlattr *nest;
  1564. if (!slwt->bpf.prog)
  1565. return 0;
  1566. nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
  1567. if (!nest)
  1568. return -EMSGSIZE;
  1569. if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
  1570. return -EMSGSIZE;
  1571. if (slwt->bpf.name &&
  1572. nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
  1573. return -EMSGSIZE;
  1574. return nla_nest_end(skb, nest);
  1575. }
  1576. static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1577. {
  1578. if (!a->bpf.name && !b->bpf.name)
  1579. return 0;
  1580. if (!a->bpf.name || !b->bpf.name)
  1581. return 1;
  1582. return strcmp(a->bpf.name, b->bpf.name);
  1583. }
  1584. static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
  1585. {
  1586. kfree(slwt->bpf.name);
  1587. if (slwt->bpf.prog)
  1588. bpf_prog_put(slwt->bpf.prog);
  1589. }
  1590. static const struct
  1591. nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
  1592. [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
  1593. [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
  1594. [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
  1595. };
  1596. static int parse_nla_counters(struct nlattr **attrs,
  1597. struct seg6_local_lwt *slwt,
  1598. struct netlink_ext_ack *extack)
  1599. {
  1600. struct pcpu_seg6_local_counters __percpu *pcounters;
  1601. struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
  1602. int ret;
  1603. ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
  1604. attrs[SEG6_LOCAL_COUNTERS],
  1605. seg6_local_counters_policy, NULL);
  1606. if (ret < 0)
  1607. return ret;
  1608. /* basic support for SRv6 Behavior counters requires at least:
  1609. * packets, bytes and errors.
  1610. */
  1611. if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
  1612. !tb[SEG6_LOCAL_CNT_ERRORS])
  1613. return -EINVAL;
  1614. /* counters are always zero initialized */
  1615. pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
  1616. if (!pcounters)
  1617. return -ENOMEM;
  1618. slwt->pcpu_counters = pcounters;
  1619. return 0;
  1620. }
  1621. static int seg6_local_fill_nla_counters(struct sk_buff *skb,
  1622. struct seg6_local_counters *counters)
  1623. {
  1624. if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
  1625. SEG6_LOCAL_CNT_PAD))
  1626. return -EMSGSIZE;
  1627. if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
  1628. SEG6_LOCAL_CNT_PAD))
  1629. return -EMSGSIZE;
  1630. if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
  1631. SEG6_LOCAL_CNT_PAD))
  1632. return -EMSGSIZE;
  1633. return 0;
  1634. }
  1635. static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1636. {
  1637. struct seg6_local_counters counters = { 0, 0, 0 };
  1638. struct nlattr *nest;
  1639. int rc, i;
  1640. nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
  1641. if (!nest)
  1642. return -EMSGSIZE;
  1643. for_each_possible_cpu(i) {
  1644. struct pcpu_seg6_local_counters *pcounters;
  1645. u64 packets, bytes, errors;
  1646. unsigned int start;
  1647. pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
  1648. do {
  1649. start = u64_stats_fetch_begin(&pcounters->syncp);
  1650. packets = u64_stats_read(&pcounters->packets);
  1651. bytes = u64_stats_read(&pcounters->bytes);
  1652. errors = u64_stats_read(&pcounters->errors);
  1653. } while (u64_stats_fetch_retry(&pcounters->syncp, start));
  1654. counters.packets += packets;
  1655. counters.bytes += bytes;
  1656. counters.errors += errors;
  1657. }
  1658. rc = seg6_local_fill_nla_counters(skb, &counters);
  1659. if (rc < 0) {
  1660. nla_nest_cancel(skb, nest);
  1661. return rc;
  1662. }
  1663. return nla_nest_end(skb, nest);
  1664. }
  1665. static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1666. {
  1667. /* a and b are equal if both have pcpu_counters set or not */
  1668. return (!!((unsigned long)a->pcpu_counters)) ^
  1669. (!!((unsigned long)b->pcpu_counters));
  1670. }
  1671. static void destroy_attr_counters(struct seg6_local_lwt *slwt)
  1672. {
  1673. free_percpu(slwt->pcpu_counters);
  1674. }
  1675. static const
  1676. struct nla_policy seg6_local_flavors_policy[SEG6_LOCAL_FLV_MAX + 1] = {
  1677. [SEG6_LOCAL_FLV_OPERATION] = { .type = NLA_U32 },
  1678. [SEG6_LOCAL_FLV_LCBLOCK_BITS] = { .type = NLA_U8 },
  1679. [SEG6_LOCAL_FLV_LCNODE_FN_BITS] = { .type = NLA_U8 },
  1680. };
  1681. /* check whether the lengths of the Locator-Block and Locator-Node Function
  1682. * are compatible with the dimension of a C-SID container.
  1683. */
  1684. static int seg6_chk_next_csid_cfg(__u8 block_len, __u8 func_len)
  1685. {
  1686. /* Locator-Block and Locator-Node Function cannot exceed 128 bits
  1687. * (i.e. C-SID container lenghts).
  1688. */
  1689. if (next_csid_chk_cntr_bits(block_len, func_len))
  1690. return -EINVAL;
  1691. /* Locator-Block length must be greater than zero and evenly divisible
  1692. * by 8. There must be room for a Locator-Node Function, at least.
  1693. */
  1694. if (next_csid_chk_lcblock_bits(block_len))
  1695. return -EINVAL;
  1696. /* Locator-Node Function length must be greater than zero and evenly
  1697. * divisible by 8. There must be room for the Locator-Block.
  1698. */
  1699. if (next_csid_chk_lcnode_fn_bits(func_len))
  1700. return -EINVAL;
  1701. return 0;
  1702. }
  1703. static int seg6_parse_nla_next_csid_cfg(struct nlattr **tb,
  1704. struct seg6_flavors_info *finfo,
  1705. struct netlink_ext_ack *extack)
  1706. {
  1707. __u8 func_len = SEG6_LOCAL_LCNODE_FN_DBITS;
  1708. __u8 block_len = SEG6_LOCAL_LCBLOCK_DBITS;
  1709. int rc;
  1710. if (tb[SEG6_LOCAL_FLV_LCBLOCK_BITS])
  1711. block_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCBLOCK_BITS]);
  1712. if (tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS])
  1713. func_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS]);
  1714. rc = seg6_chk_next_csid_cfg(block_len, func_len);
  1715. if (rc < 0) {
  1716. NL_SET_ERR_MSG(extack,
  1717. "Invalid Locator Block/Node Function lengths");
  1718. return rc;
  1719. }
  1720. finfo->lcblock_bits = block_len;
  1721. finfo->lcnode_func_bits = func_len;
  1722. return 0;
  1723. }
  1724. static int parse_nla_flavors(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1725. struct netlink_ext_ack *extack)
  1726. {
  1727. struct seg6_flavors_info *finfo = &slwt->flv_info;
  1728. struct nlattr *tb[SEG6_LOCAL_FLV_MAX + 1];
  1729. int action = slwt->action;
  1730. __u32 fops, supp_fops;
  1731. int rc;
  1732. rc = nla_parse_nested_deprecated(tb, SEG6_LOCAL_FLV_MAX,
  1733. attrs[SEG6_LOCAL_FLAVORS],
  1734. seg6_local_flavors_policy, NULL);
  1735. if (rc < 0)
  1736. return rc;
  1737. /* this attribute MUST always be present since it represents the Flavor
  1738. * operation(s) to be carried out.
  1739. */
  1740. if (!tb[SEG6_LOCAL_FLV_OPERATION])
  1741. return -EINVAL;
  1742. fops = nla_get_u32(tb[SEG6_LOCAL_FLV_OPERATION]);
  1743. rc = seg6_flv_supp_ops_by_action(action, &supp_fops);
  1744. if (rc < 0 || (fops & ~supp_fops)) {
  1745. NL_SET_ERR_MSG(extack, "Unsupported Flavor operation(s)");
  1746. return -EOPNOTSUPP;
  1747. }
  1748. finfo->flv_ops = fops;
  1749. if (seg6_next_csid_enabled(fops)) {
  1750. /* Locator-Block and Locator-Node Function lengths can be
  1751. * provided by the user space. Otherwise, default values are
  1752. * applied.
  1753. */
  1754. rc = seg6_parse_nla_next_csid_cfg(tb, finfo, extack);
  1755. if (rc < 0)
  1756. return rc;
  1757. }
  1758. return 0;
  1759. }
  1760. static int seg6_fill_nla_next_csid_cfg(struct sk_buff *skb,
  1761. struct seg6_flavors_info *finfo)
  1762. {
  1763. if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCBLOCK_BITS, finfo->lcblock_bits))
  1764. return -EMSGSIZE;
  1765. if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCNODE_FN_BITS,
  1766. finfo->lcnode_func_bits))
  1767. return -EMSGSIZE;
  1768. return 0;
  1769. }
  1770. static int put_nla_flavors(struct sk_buff *skb, struct seg6_local_lwt *slwt)
  1771. {
  1772. struct seg6_flavors_info *finfo = &slwt->flv_info;
  1773. __u32 fops = finfo->flv_ops;
  1774. struct nlattr *nest;
  1775. int rc;
  1776. nest = nla_nest_start(skb, SEG6_LOCAL_FLAVORS);
  1777. if (!nest)
  1778. return -EMSGSIZE;
  1779. if (nla_put_u32(skb, SEG6_LOCAL_FLV_OPERATION, fops)) {
  1780. rc = -EMSGSIZE;
  1781. goto err;
  1782. }
  1783. if (seg6_next_csid_enabled(fops)) {
  1784. rc = seg6_fill_nla_next_csid_cfg(skb, finfo);
  1785. if (rc < 0)
  1786. goto err;
  1787. }
  1788. return nla_nest_end(skb, nest);
  1789. err:
  1790. nla_nest_cancel(skb, nest);
  1791. return rc;
  1792. }
  1793. static int seg6_cmp_nla_next_csid_cfg(struct seg6_flavors_info *finfo_a,
  1794. struct seg6_flavors_info *finfo_b)
  1795. {
  1796. if (finfo_a->lcblock_bits != finfo_b->lcblock_bits)
  1797. return 1;
  1798. if (finfo_a->lcnode_func_bits != finfo_b->lcnode_func_bits)
  1799. return 1;
  1800. return 0;
  1801. }
  1802. static int cmp_nla_flavors(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
  1803. {
  1804. struct seg6_flavors_info *finfo_a = &a->flv_info;
  1805. struct seg6_flavors_info *finfo_b = &b->flv_info;
  1806. if (finfo_a->flv_ops != finfo_b->flv_ops)
  1807. return 1;
  1808. if (seg6_next_csid_enabled(finfo_a->flv_ops)) {
  1809. if (seg6_cmp_nla_next_csid_cfg(finfo_a, finfo_b))
  1810. return 1;
  1811. }
  1812. return 0;
  1813. }
  1814. static int encap_size_flavors(struct seg6_local_lwt *slwt)
  1815. {
  1816. struct seg6_flavors_info *finfo = &slwt->flv_info;
  1817. int nlsize;
  1818. nlsize = nla_total_size(0) + /* nest SEG6_LOCAL_FLAVORS */
  1819. nla_total_size(4); /* SEG6_LOCAL_FLV_OPERATION */
  1820. if (seg6_next_csid_enabled(finfo->flv_ops))
  1821. nlsize += nla_total_size(1) + /* SEG6_LOCAL_FLV_LCBLOCK_BITS */
  1822. nla_total_size(1); /* SEG6_LOCAL_FLV_LCNODE_FN_BITS */
  1823. return nlsize;
  1824. }
  1825. struct seg6_action_param {
  1826. int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1827. struct netlink_ext_ack *extack);
  1828. int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
  1829. int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
  1830. /* optional destroy() callback useful for releasing resources which
  1831. * have been previously acquired in the corresponding parse()
  1832. * function.
  1833. */
  1834. void (*destroy)(struct seg6_local_lwt *slwt);
  1835. };
  1836. static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
  1837. [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
  1838. .put = put_nla_srh,
  1839. .cmp = cmp_nla_srh,
  1840. .destroy = destroy_attr_srh },
  1841. [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
  1842. .put = put_nla_table,
  1843. .cmp = cmp_nla_table },
  1844. [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
  1845. .put = put_nla_nh4,
  1846. .cmp = cmp_nla_nh4 },
  1847. [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
  1848. .put = put_nla_nh6,
  1849. .cmp = cmp_nla_nh6 },
  1850. [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
  1851. .put = put_nla_iif,
  1852. .cmp = cmp_nla_iif },
  1853. [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
  1854. .put = put_nla_oif,
  1855. .cmp = cmp_nla_oif },
  1856. [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
  1857. .put = put_nla_bpf,
  1858. .cmp = cmp_nla_bpf,
  1859. .destroy = destroy_attr_bpf },
  1860. [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
  1861. .put = put_nla_vrftable,
  1862. .cmp = cmp_nla_vrftable },
  1863. [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
  1864. .put = put_nla_counters,
  1865. .cmp = cmp_nla_counters,
  1866. .destroy = destroy_attr_counters },
  1867. [SEG6_LOCAL_FLAVORS] = { .parse = parse_nla_flavors,
  1868. .put = put_nla_flavors,
  1869. .cmp = cmp_nla_flavors },
  1870. };
  1871. /* call the destroy() callback (if available) for each set attribute in
  1872. * @parsed_attrs, starting from the first attribute up to the @max_parsed
  1873. * (excluded) attribute.
  1874. */
  1875. static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
  1876. struct seg6_local_lwt *slwt)
  1877. {
  1878. struct seg6_action_param *param;
  1879. int i;
  1880. /* Every required seg6local attribute is identified by an ID which is
  1881. * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
  1882. *
  1883. * We scan the 'parsed_attrs' bitmask, starting from the first attribute
  1884. * up to the @max_parsed (excluded) attribute.
  1885. * For each set attribute, we retrieve the corresponding destroy()
  1886. * callback. If the callback is not available, then we skip to the next
  1887. * attribute; otherwise, we call the destroy() callback.
  1888. */
  1889. for (i = SEG6_LOCAL_SRH; i < max_parsed; ++i) {
  1890. if (!(parsed_attrs & SEG6_F_ATTR(i)))
  1891. continue;
  1892. param = &seg6_action_params[i];
  1893. if (param->destroy)
  1894. param->destroy(slwt);
  1895. }
  1896. }
  1897. /* release all the resources that may have been acquired during parsing
  1898. * operations.
  1899. */
  1900. static void destroy_attrs(struct seg6_local_lwt *slwt)
  1901. {
  1902. unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
  1903. __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
  1904. }
  1905. static int parse_nla_optional_attrs(struct nlattr **attrs,
  1906. struct seg6_local_lwt *slwt,
  1907. struct netlink_ext_ack *extack)
  1908. {
  1909. struct seg6_action_desc *desc = slwt->desc;
  1910. unsigned long parsed_optattrs = 0;
  1911. struct seg6_action_param *param;
  1912. int err, i;
  1913. for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; ++i) {
  1914. if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
  1915. continue;
  1916. /* once here, the i-th attribute is provided by the
  1917. * userspace AND it is identified optional as well.
  1918. */
  1919. param = &seg6_action_params[i];
  1920. err = param->parse(attrs, slwt, extack);
  1921. if (err < 0)
  1922. goto parse_optattrs_err;
  1923. /* current attribute has been correctly parsed */
  1924. parsed_optattrs |= SEG6_F_ATTR(i);
  1925. }
  1926. /* store in the tunnel state all the optional attributed successfully
  1927. * parsed.
  1928. */
  1929. slwt->parsed_optattrs = parsed_optattrs;
  1930. return 0;
  1931. parse_optattrs_err:
  1932. __destroy_attrs(parsed_optattrs, i, slwt);
  1933. return err;
  1934. }
  1935. /* call the custom constructor of the behavior during its initialization phase
  1936. * and after that all its attributes have been parsed successfully.
  1937. */
  1938. static int
  1939. seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
  1940. struct netlink_ext_ack *extack)
  1941. {
  1942. struct seg6_action_desc *desc = slwt->desc;
  1943. struct seg6_local_lwtunnel_ops *ops;
  1944. ops = &desc->slwt_ops;
  1945. if (!ops->build_state)
  1946. return 0;
  1947. return ops->build_state(slwt, cfg, extack);
  1948. }
  1949. /* call the custom destructor of the behavior which is invoked before the
  1950. * tunnel is going to be destroyed.
  1951. */
  1952. static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
  1953. {
  1954. struct seg6_action_desc *desc = slwt->desc;
  1955. struct seg6_local_lwtunnel_ops *ops;
  1956. ops = &desc->slwt_ops;
  1957. if (!ops->destroy_state)
  1958. return;
  1959. ops->destroy_state(slwt);
  1960. }
  1961. static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt,
  1962. struct netlink_ext_ack *extack)
  1963. {
  1964. struct seg6_action_param *param;
  1965. struct seg6_action_desc *desc;
  1966. unsigned long invalid_attrs;
  1967. int i, err;
  1968. desc = __get_action_desc(slwt->action);
  1969. if (!desc)
  1970. return -EINVAL;
  1971. if (!desc->input)
  1972. return -EOPNOTSUPP;
  1973. slwt->desc = desc;
  1974. slwt->headroom += desc->static_headroom;
  1975. /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
  1976. * disjoined, this allow us to release acquired resources by optional
  1977. * attributes and by required attributes independently from each other
  1978. * without any interference.
  1979. * In other terms, we are sure that we do not release some the acquired
  1980. * resources twice.
  1981. *
  1982. * Note that if an attribute is configured both as required and as
  1983. * optional, it means that the user has messed something up in the
  1984. * seg6_action_table. Therefore, this check is required for SRv6
  1985. * behaviors to work properly.
  1986. */
  1987. invalid_attrs = desc->attrs & desc->optattrs;
  1988. if (invalid_attrs) {
  1989. WARN_ONCE(1,
  1990. "An attribute cannot be both required AND optional");
  1991. return -EINVAL;
  1992. }
  1993. /* parse the required attributes */
  1994. for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
  1995. if (desc->attrs & SEG6_F_ATTR(i)) {
  1996. if (!attrs[i])
  1997. return -EINVAL;
  1998. param = &seg6_action_params[i];
  1999. err = param->parse(attrs, slwt, extack);
  2000. if (err < 0)
  2001. goto parse_attrs_err;
  2002. }
  2003. }
  2004. /* parse the optional attributes, if any */
  2005. err = parse_nla_optional_attrs(attrs, slwt, extack);
  2006. if (err < 0)
  2007. goto parse_attrs_err;
  2008. return 0;
  2009. parse_attrs_err:
  2010. /* release any resource that may have been acquired during the i-1
  2011. * parse() operations.
  2012. */
  2013. __destroy_attrs(desc->attrs, i, slwt);
  2014. return err;
  2015. }
  2016. static int seg6_local_build_state(struct net *net, struct nlattr *nla,
  2017. unsigned int family, const void *cfg,
  2018. struct lwtunnel_state **ts,
  2019. struct netlink_ext_ack *extack)
  2020. {
  2021. struct nlattr *tb[SEG6_LOCAL_MAX + 1];
  2022. struct lwtunnel_state *newts;
  2023. struct seg6_local_lwt *slwt;
  2024. int err;
  2025. if (family != AF_INET6)
  2026. return -EINVAL;
  2027. err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
  2028. seg6_local_policy, extack);
  2029. if (err < 0)
  2030. return err;
  2031. if (!tb[SEG6_LOCAL_ACTION])
  2032. return -EINVAL;
  2033. newts = lwtunnel_state_alloc(sizeof(*slwt));
  2034. if (!newts)
  2035. return -ENOMEM;
  2036. slwt = seg6_local_lwtunnel(newts);
  2037. slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
  2038. err = parse_nla_action(tb, slwt, extack);
  2039. if (err < 0)
  2040. goto out_free;
  2041. err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
  2042. if (err < 0)
  2043. goto out_destroy_attrs;
  2044. newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
  2045. newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
  2046. newts->headroom = slwt->headroom;
  2047. *ts = newts;
  2048. return 0;
  2049. out_destroy_attrs:
  2050. destroy_attrs(slwt);
  2051. out_free:
  2052. kfree(newts);
  2053. return err;
  2054. }
  2055. static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
  2056. {
  2057. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  2058. seg6_local_lwtunnel_destroy_state(slwt);
  2059. destroy_attrs(slwt);
  2060. return;
  2061. }
  2062. static int seg6_local_fill_encap(struct sk_buff *skb,
  2063. struct lwtunnel_state *lwt)
  2064. {
  2065. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  2066. struct seg6_action_param *param;
  2067. unsigned long attrs;
  2068. int i, err;
  2069. if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
  2070. return -EMSGSIZE;
  2071. attrs = slwt->desc->attrs | slwt->parsed_optattrs;
  2072. for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
  2073. if (attrs & SEG6_F_ATTR(i)) {
  2074. param = &seg6_action_params[i];
  2075. err = param->put(skb, slwt);
  2076. if (err < 0)
  2077. return err;
  2078. }
  2079. }
  2080. return 0;
  2081. }
  2082. static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
  2083. {
  2084. struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
  2085. unsigned long attrs;
  2086. int nlsize;
  2087. nlsize = nla_total_size(4); /* action */
  2088. attrs = slwt->desc->attrs | slwt->parsed_optattrs;
  2089. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
  2090. nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
  2091. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
  2092. nlsize += nla_total_size(4);
  2093. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
  2094. nlsize += nla_total_size(4);
  2095. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
  2096. nlsize += nla_total_size(16);
  2097. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
  2098. nlsize += nla_total_size(4);
  2099. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
  2100. nlsize += nla_total_size(4);
  2101. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
  2102. nlsize += nla_total_size(sizeof(struct nlattr)) +
  2103. nla_total_size(MAX_PROG_NAME) +
  2104. nla_total_size(4);
  2105. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
  2106. nlsize += nla_total_size(4);
  2107. if (attrs & SEG6_F_LOCAL_COUNTERS)
  2108. nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
  2109. /* SEG6_LOCAL_CNT_PACKETS */
  2110. nla_total_size_64bit(sizeof(__u64)) +
  2111. /* SEG6_LOCAL_CNT_BYTES */
  2112. nla_total_size_64bit(sizeof(__u64)) +
  2113. /* SEG6_LOCAL_CNT_ERRORS */
  2114. nla_total_size_64bit(sizeof(__u64));
  2115. if (attrs & SEG6_F_ATTR(SEG6_LOCAL_FLAVORS))
  2116. nlsize += encap_size_flavors(slwt);
  2117. return nlsize;
  2118. }
  2119. static int seg6_local_cmp_encap(struct lwtunnel_state *a,
  2120. struct lwtunnel_state *b)
  2121. {
  2122. struct seg6_local_lwt *slwt_a, *slwt_b;
  2123. struct seg6_action_param *param;
  2124. unsigned long attrs_a, attrs_b;
  2125. int i;
  2126. slwt_a = seg6_local_lwtunnel(a);
  2127. slwt_b = seg6_local_lwtunnel(b);
  2128. if (slwt_a->action != slwt_b->action)
  2129. return 1;
  2130. attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
  2131. attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
  2132. if (attrs_a != attrs_b)
  2133. return 1;
  2134. for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
  2135. if (attrs_a & SEG6_F_ATTR(i)) {
  2136. param = &seg6_action_params[i];
  2137. if (param->cmp(slwt_a, slwt_b))
  2138. return 1;
  2139. }
  2140. }
  2141. return 0;
  2142. }
  2143. static const struct lwtunnel_encap_ops seg6_local_ops = {
  2144. .build_state = seg6_local_build_state,
  2145. .destroy_state = seg6_local_destroy_state,
  2146. .input = seg6_local_input,
  2147. .fill_encap = seg6_local_fill_encap,
  2148. .get_encap_size = seg6_local_get_encap_size,
  2149. .cmp_encap = seg6_local_cmp_encap,
  2150. .owner = THIS_MODULE,
  2151. };
  2152. int __init seg6_local_init(void)
  2153. {
  2154. /* If the max total number of defined attributes is reached, then your
  2155. * kernel build stops here.
  2156. *
  2157. * This check is required to avoid arithmetic overflows when processing
  2158. * behavior attributes and the maximum number of defined attributes
  2159. * exceeds the allowed value.
  2160. */
  2161. BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
  2162. /* Check whether the number of defined flavors exceeds the maximum
  2163. * allowed value.
  2164. */
  2165. BUILD_BUG_ON(SEG6_LOCAL_FLV_OP_MAX + 1 > BITS_PER_TYPE(__u32));
  2166. /* If the default NEXT-C-SID Locator-Block/Node Function lengths (in
  2167. * bits) have been changed with invalid values, kernel build stops
  2168. * here.
  2169. */
  2170. BUILD_BUG_ON(next_csid_chk_cntr_bits(SEG6_LOCAL_LCBLOCK_DBITS,
  2171. SEG6_LOCAL_LCNODE_FN_DBITS));
  2172. BUILD_BUG_ON(next_csid_chk_lcblock_bits(SEG6_LOCAL_LCBLOCK_DBITS));
  2173. BUILD_BUG_ON(next_csid_chk_lcnode_fn_bits(SEG6_LOCAL_LCNODE_FN_DBITS));
  2174. /* To be memory efficient, we use 'u8' to represent the different
  2175. * actions related to RFC8986 flavors. If the kernel build stops here,
  2176. * it means that it is not possible to correctly encode these actions
  2177. * with the data type chosen for the action table.
  2178. */
  2179. BUILD_BUG_ON(SEG6_LOCAL_FLV_ACT_MAX > (typeof(flv8986_act_tbl[0]))~0U);
  2180. return lwtunnel_encap_add_ops(&seg6_local_ops,
  2181. LWTUNNEL_ENCAP_SEG6_LOCAL);
  2182. }
  2183. void seg6_local_exit(void)
  2184. {
  2185. lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
  2186. }