macsec.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/macsec.c - MACsec device
  4. *
  5. * Copyright (c) 2015 Sabrina Dubroca <[email protected]>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/socket.h>
  10. #include <linux/module.h>
  11. #include <crypto/aead.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/refcount.h>
  16. #include <net/genetlink.h>
  17. #include <net/sock.h>
  18. #include <net/gro_cells.h>
  19. #include <net/macsec.h>
  20. #include <net/dst_metadata.h>
  21. #include <linux/phy.h>
  22. #include <linux/byteorder/generic.h>
  23. #include <linux/if_arp.h>
  24. #include <uapi/linux/if_macsec.h>
  25. /* SecTAG length = macsec_eth_header without the optional SCI */
  26. #define MACSEC_TAG_LEN 6
  27. struct macsec_eth_header {
  28. struct ethhdr eth;
  29. /* SecTAG */
  30. u8 tci_an;
  31. #if defined(__LITTLE_ENDIAN_BITFIELD)
  32. u8 short_length:6,
  33. unused:2;
  34. #elif defined(__BIG_ENDIAN_BITFIELD)
  35. u8 unused:2,
  36. short_length:6;
  37. #else
  38. #error "Please fix <asm/byteorder.h>"
  39. #endif
  40. __be32 packet_number;
  41. u8 secure_channel_id[8]; /* optional */
  42. } __packed;
  43. /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
  44. #define MIN_NON_SHORT_LEN 48
  45. #define GCM_AES_IV_LEN 12
  46. #define for_each_rxsc(secy, sc) \
  47. for (sc = rcu_dereference_bh(secy->rx_sc); \
  48. sc; \
  49. sc = rcu_dereference_bh(sc->next))
  50. #define for_each_rxsc_rtnl(secy, sc) \
  51. for (sc = rtnl_dereference(secy->rx_sc); \
  52. sc; \
  53. sc = rtnl_dereference(sc->next))
  54. #define pn_same_half(pn1, pn2) (!(((pn1) >> 31) ^ ((pn2) >> 31)))
  55. struct gcm_iv_xpn {
  56. union {
  57. u8 short_secure_channel_id[4];
  58. ssci_t ssci;
  59. };
  60. __be64 pn;
  61. } __packed;
  62. struct gcm_iv {
  63. union {
  64. u8 secure_channel_id[8];
  65. sci_t sci;
  66. };
  67. __be32 pn;
  68. };
  69. #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
  70. struct pcpu_secy_stats {
  71. struct macsec_dev_stats stats;
  72. struct u64_stats_sync syncp;
  73. };
  74. /**
  75. * struct macsec_dev - private data
  76. * @secy: SecY config
  77. * @real_dev: pointer to underlying netdevice
  78. * @dev_tracker: refcount tracker for @real_dev reference
  79. * @stats: MACsec device stats
  80. * @secys: linked list of SecY's on the underlying device
  81. * @gro_cells: pointer to the Generic Receive Offload cell
  82. * @offload: status of offloading on the MACsec device
  83. */
  84. struct macsec_dev {
  85. struct macsec_secy secy;
  86. struct net_device *real_dev;
  87. netdevice_tracker dev_tracker;
  88. struct pcpu_secy_stats __percpu *stats;
  89. struct list_head secys;
  90. struct gro_cells gro_cells;
  91. enum macsec_offload offload;
  92. };
  93. /**
  94. * struct macsec_rxh_data - rx_handler private argument
  95. * @secys: linked list of SecY's on this underlying device
  96. */
  97. struct macsec_rxh_data {
  98. struct list_head secys;
  99. };
  100. static struct macsec_dev *macsec_priv(const struct net_device *dev)
  101. {
  102. return (struct macsec_dev *)netdev_priv(dev);
  103. }
  104. static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
  105. {
  106. return rcu_dereference_bh(dev->rx_handler_data);
  107. }
  108. static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
  109. {
  110. return rtnl_dereference(dev->rx_handler_data);
  111. }
  112. struct macsec_cb {
  113. struct aead_request *req;
  114. union {
  115. struct macsec_tx_sa *tx_sa;
  116. struct macsec_rx_sa *rx_sa;
  117. };
  118. u8 assoc_num;
  119. bool valid;
  120. bool has_sci;
  121. };
  122. static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
  123. {
  124. struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
  125. if (!sa || !sa->active)
  126. return NULL;
  127. if (!refcount_inc_not_zero(&sa->refcnt))
  128. return NULL;
  129. return sa;
  130. }
  131. static struct macsec_rx_sa *macsec_active_rxsa_get(struct macsec_rx_sc *rx_sc)
  132. {
  133. struct macsec_rx_sa *sa = NULL;
  134. int an;
  135. for (an = 0; an < MACSEC_NUM_AN; an++) {
  136. sa = macsec_rxsa_get(rx_sc->sa[an]);
  137. if (sa)
  138. break;
  139. }
  140. return sa;
  141. }
  142. static void free_rx_sc_rcu(struct rcu_head *head)
  143. {
  144. struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
  145. free_percpu(rx_sc->stats);
  146. kfree(rx_sc);
  147. }
  148. static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
  149. {
  150. return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
  151. }
  152. static void macsec_rxsc_put(struct macsec_rx_sc *sc)
  153. {
  154. if (refcount_dec_and_test(&sc->refcnt))
  155. call_rcu(&sc->rcu_head, free_rx_sc_rcu);
  156. }
  157. static void free_rxsa(struct rcu_head *head)
  158. {
  159. struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
  160. crypto_free_aead(sa->key.tfm);
  161. free_percpu(sa->stats);
  162. kfree(sa);
  163. }
  164. static void macsec_rxsa_put(struct macsec_rx_sa *sa)
  165. {
  166. if (refcount_dec_and_test(&sa->refcnt))
  167. call_rcu(&sa->rcu, free_rxsa);
  168. }
  169. static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
  170. {
  171. struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
  172. if (!sa || !sa->active)
  173. return NULL;
  174. if (!refcount_inc_not_zero(&sa->refcnt))
  175. return NULL;
  176. return sa;
  177. }
  178. static void free_txsa(struct rcu_head *head)
  179. {
  180. struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
  181. crypto_free_aead(sa->key.tfm);
  182. free_percpu(sa->stats);
  183. kfree(sa);
  184. }
  185. static void macsec_txsa_put(struct macsec_tx_sa *sa)
  186. {
  187. if (refcount_dec_and_test(&sa->refcnt))
  188. call_rcu(&sa->rcu, free_txsa);
  189. }
  190. static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
  191. {
  192. BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
  193. return (struct macsec_cb *)skb->cb;
  194. }
  195. #define MACSEC_PORT_SCB (0x0000)
  196. #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
  197. #define MACSEC_UNDEF_SSCI ((__force ssci_t)0xffffffff)
  198. #define MACSEC_GCM_AES_128_SAK_LEN 16
  199. #define MACSEC_GCM_AES_256_SAK_LEN 32
  200. #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
  201. #define DEFAULT_XPN false
  202. #define DEFAULT_SEND_SCI true
  203. #define DEFAULT_ENCRYPT false
  204. #define DEFAULT_ENCODING_SA 0
  205. #define MACSEC_XPN_MAX_REPLAY_WINDOW (((1 << 30) - 1))
  206. static sci_t make_sci(const u8 *addr, __be16 port)
  207. {
  208. sci_t sci;
  209. memcpy(&sci, addr, ETH_ALEN);
  210. memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
  211. return sci;
  212. }
  213. static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
  214. {
  215. sci_t sci;
  216. if (sci_present)
  217. memcpy(&sci, hdr->secure_channel_id,
  218. sizeof(hdr->secure_channel_id));
  219. else
  220. sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
  221. return sci;
  222. }
  223. static unsigned int macsec_sectag_len(bool sci_present)
  224. {
  225. return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
  226. }
  227. static unsigned int macsec_hdr_len(bool sci_present)
  228. {
  229. return macsec_sectag_len(sci_present) + ETH_HLEN;
  230. }
  231. static unsigned int macsec_extra_len(bool sci_present)
  232. {
  233. return macsec_sectag_len(sci_present) + sizeof(__be16);
  234. }
  235. /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
  236. static void macsec_fill_sectag(struct macsec_eth_header *h,
  237. const struct macsec_secy *secy, u32 pn,
  238. bool sci_present)
  239. {
  240. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  241. memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
  242. h->eth.h_proto = htons(ETH_P_MACSEC);
  243. if (sci_present) {
  244. h->tci_an |= MACSEC_TCI_SC;
  245. memcpy(&h->secure_channel_id, &secy->sci,
  246. sizeof(h->secure_channel_id));
  247. } else {
  248. if (tx_sc->end_station)
  249. h->tci_an |= MACSEC_TCI_ES;
  250. if (tx_sc->scb)
  251. h->tci_an |= MACSEC_TCI_SCB;
  252. }
  253. h->packet_number = htonl(pn);
  254. /* with GCM, C/E clear for !encrypt, both set for encrypt */
  255. if (tx_sc->encrypt)
  256. h->tci_an |= MACSEC_TCI_CONFID;
  257. else if (secy->icv_len != MACSEC_DEFAULT_ICV_LEN)
  258. h->tci_an |= MACSEC_TCI_C;
  259. h->tci_an |= tx_sc->encoding_sa;
  260. }
  261. static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
  262. {
  263. if (data_len < MIN_NON_SHORT_LEN)
  264. h->short_length = data_len;
  265. }
  266. /* Checks if a MACsec interface is being offloaded to an hardware engine */
  267. static bool macsec_is_offloaded(struct macsec_dev *macsec)
  268. {
  269. if (macsec->offload == MACSEC_OFFLOAD_MAC ||
  270. macsec->offload == MACSEC_OFFLOAD_PHY)
  271. return true;
  272. return false;
  273. }
  274. /* Checks if underlying layers implement MACsec offloading functions. */
  275. static bool macsec_check_offload(enum macsec_offload offload,
  276. struct macsec_dev *macsec)
  277. {
  278. if (!macsec || !macsec->real_dev)
  279. return false;
  280. if (offload == MACSEC_OFFLOAD_PHY)
  281. return macsec->real_dev->phydev &&
  282. macsec->real_dev->phydev->macsec_ops;
  283. else if (offload == MACSEC_OFFLOAD_MAC)
  284. return macsec->real_dev->features & NETIF_F_HW_MACSEC &&
  285. macsec->real_dev->macsec_ops;
  286. return false;
  287. }
  288. static const struct macsec_ops *__macsec_get_ops(enum macsec_offload offload,
  289. struct macsec_dev *macsec,
  290. struct macsec_context *ctx)
  291. {
  292. if (ctx) {
  293. memset(ctx, 0, sizeof(*ctx));
  294. ctx->offload = offload;
  295. if (offload == MACSEC_OFFLOAD_PHY)
  296. ctx->phydev = macsec->real_dev->phydev;
  297. else if (offload == MACSEC_OFFLOAD_MAC)
  298. ctx->netdev = macsec->real_dev;
  299. }
  300. if (offload == MACSEC_OFFLOAD_PHY)
  301. return macsec->real_dev->phydev->macsec_ops;
  302. else
  303. return macsec->real_dev->macsec_ops;
  304. }
  305. /* Returns a pointer to the MACsec ops struct if any and updates the MACsec
  306. * context device reference if provided.
  307. */
  308. static const struct macsec_ops *macsec_get_ops(struct macsec_dev *macsec,
  309. struct macsec_context *ctx)
  310. {
  311. if (!macsec_check_offload(macsec->offload, macsec))
  312. return NULL;
  313. return __macsec_get_ops(macsec->offload, macsec, ctx);
  314. }
  315. /* validate MACsec packet according to IEEE 802.1AE-2018 9.12 */
  316. static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len, bool xpn)
  317. {
  318. struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
  319. int len = skb->len - 2 * ETH_ALEN;
  320. int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
  321. /* a) It comprises at least 17 octets */
  322. if (skb->len <= 16)
  323. return false;
  324. /* b) MACsec EtherType: already checked */
  325. /* c) V bit is clear */
  326. if (h->tci_an & MACSEC_TCI_VERSION)
  327. return false;
  328. /* d) ES or SCB => !SC */
  329. if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
  330. (h->tci_an & MACSEC_TCI_SC))
  331. return false;
  332. /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
  333. if (h->unused)
  334. return false;
  335. /* rx.pn != 0 if not XPN (figure 10-5 with 802.11AEbw-2013 amendment) */
  336. if (!h->packet_number && !xpn)
  337. return false;
  338. /* length check, f) g) h) i) */
  339. if (h->short_length)
  340. return len == extra_len + h->short_length;
  341. return len >= extra_len + MIN_NON_SHORT_LEN;
  342. }
  343. #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
  344. #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
  345. static void macsec_fill_iv_xpn(unsigned char *iv, ssci_t ssci, u64 pn,
  346. salt_t salt)
  347. {
  348. struct gcm_iv_xpn *gcm_iv = (struct gcm_iv_xpn *)iv;
  349. gcm_iv->ssci = ssci ^ salt.ssci;
  350. gcm_iv->pn = cpu_to_be64(pn) ^ salt.pn;
  351. }
  352. static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
  353. {
  354. struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
  355. gcm_iv->sci = sci;
  356. gcm_iv->pn = htonl(pn);
  357. }
  358. static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
  359. {
  360. return (struct macsec_eth_header *)skb_mac_header(skb);
  361. }
  362. static void __macsec_pn_wrapped(struct macsec_secy *secy,
  363. struct macsec_tx_sa *tx_sa)
  364. {
  365. pr_debug("PN wrapped, transitioning to !oper\n");
  366. tx_sa->active = false;
  367. if (secy->protect_frames)
  368. secy->operational = false;
  369. }
  370. void macsec_pn_wrapped(struct macsec_secy *secy, struct macsec_tx_sa *tx_sa)
  371. {
  372. spin_lock_bh(&tx_sa->lock);
  373. __macsec_pn_wrapped(secy, tx_sa);
  374. spin_unlock_bh(&tx_sa->lock);
  375. }
  376. EXPORT_SYMBOL_GPL(macsec_pn_wrapped);
  377. static pn_t tx_sa_update_pn(struct macsec_tx_sa *tx_sa,
  378. struct macsec_secy *secy)
  379. {
  380. pn_t pn;
  381. spin_lock_bh(&tx_sa->lock);
  382. pn = tx_sa->next_pn_halves;
  383. if (secy->xpn)
  384. tx_sa->next_pn++;
  385. else
  386. tx_sa->next_pn_halves.lower++;
  387. if (tx_sa->next_pn == 0)
  388. __macsec_pn_wrapped(secy, tx_sa);
  389. spin_unlock_bh(&tx_sa->lock);
  390. return pn;
  391. }
  392. static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
  393. {
  394. struct macsec_dev *macsec = netdev_priv(dev);
  395. skb->dev = macsec->real_dev;
  396. skb_reset_mac_header(skb);
  397. skb->protocol = eth_hdr(skb)->h_proto;
  398. }
  399. static unsigned int macsec_msdu_len(struct sk_buff *skb)
  400. {
  401. struct macsec_dev *macsec = macsec_priv(skb->dev);
  402. struct macsec_secy *secy = &macsec->secy;
  403. bool sci_present = macsec_skb_cb(skb)->has_sci;
  404. return skb->len - macsec_hdr_len(sci_present) - secy->icv_len;
  405. }
  406. static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
  407. struct macsec_tx_sa *tx_sa)
  408. {
  409. unsigned int msdu_len = macsec_msdu_len(skb);
  410. struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
  411. u64_stats_update_begin(&txsc_stats->syncp);
  412. if (tx_sc->encrypt) {
  413. txsc_stats->stats.OutOctetsEncrypted += msdu_len;
  414. txsc_stats->stats.OutPktsEncrypted++;
  415. this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
  416. } else {
  417. txsc_stats->stats.OutOctetsProtected += msdu_len;
  418. txsc_stats->stats.OutPktsProtected++;
  419. this_cpu_inc(tx_sa->stats->OutPktsProtected);
  420. }
  421. u64_stats_update_end(&txsc_stats->syncp);
  422. }
  423. static void count_tx(struct net_device *dev, int ret, int len)
  424. {
  425. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  426. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  427. u64_stats_update_begin(&stats->syncp);
  428. u64_stats_inc(&stats->tx_packets);
  429. u64_stats_add(&stats->tx_bytes, len);
  430. u64_stats_update_end(&stats->syncp);
  431. }
  432. }
  433. static void macsec_encrypt_done(struct crypto_async_request *base, int err)
  434. {
  435. struct sk_buff *skb = base->data;
  436. struct net_device *dev = skb->dev;
  437. struct macsec_dev *macsec = macsec_priv(dev);
  438. struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
  439. int len, ret;
  440. aead_request_free(macsec_skb_cb(skb)->req);
  441. rcu_read_lock_bh();
  442. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  443. /* packet is encrypted/protected so tx_bytes must be calculated */
  444. len = macsec_msdu_len(skb) + 2 * ETH_ALEN;
  445. macsec_encrypt_finish(skb, dev);
  446. ret = dev_queue_xmit(skb);
  447. count_tx(dev, ret, len);
  448. rcu_read_unlock_bh();
  449. macsec_txsa_put(sa);
  450. dev_put(dev);
  451. }
  452. static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
  453. unsigned char **iv,
  454. struct scatterlist **sg,
  455. int num_frags)
  456. {
  457. size_t size, iv_offset, sg_offset;
  458. struct aead_request *req;
  459. void *tmp;
  460. size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
  461. iv_offset = size;
  462. size += GCM_AES_IV_LEN;
  463. size = ALIGN(size, __alignof__(struct scatterlist));
  464. sg_offset = size;
  465. size += sizeof(struct scatterlist) * num_frags;
  466. tmp = kmalloc(size, GFP_ATOMIC);
  467. if (!tmp)
  468. return NULL;
  469. *iv = (unsigned char *)(tmp + iv_offset);
  470. *sg = (struct scatterlist *)(tmp + sg_offset);
  471. req = tmp;
  472. aead_request_set_tfm(req, tfm);
  473. return req;
  474. }
  475. static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
  476. struct net_device *dev)
  477. {
  478. int ret;
  479. struct scatterlist *sg;
  480. struct sk_buff *trailer;
  481. unsigned char *iv;
  482. struct ethhdr *eth;
  483. struct macsec_eth_header *hh;
  484. size_t unprotected_len;
  485. struct aead_request *req;
  486. struct macsec_secy *secy;
  487. struct macsec_tx_sc *tx_sc;
  488. struct macsec_tx_sa *tx_sa;
  489. struct macsec_dev *macsec = macsec_priv(dev);
  490. bool sci_present;
  491. pn_t pn;
  492. secy = &macsec->secy;
  493. tx_sc = &secy->tx_sc;
  494. /* 10.5.1 TX SA assignment */
  495. tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
  496. if (!tx_sa) {
  497. secy->operational = false;
  498. kfree_skb(skb);
  499. return ERR_PTR(-EINVAL);
  500. }
  501. if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
  502. skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
  503. struct sk_buff *nskb = skb_copy_expand(skb,
  504. MACSEC_NEEDED_HEADROOM,
  505. MACSEC_NEEDED_TAILROOM,
  506. GFP_ATOMIC);
  507. if (likely(nskb)) {
  508. consume_skb(skb);
  509. skb = nskb;
  510. } else {
  511. macsec_txsa_put(tx_sa);
  512. kfree_skb(skb);
  513. return ERR_PTR(-ENOMEM);
  514. }
  515. } else {
  516. skb = skb_unshare(skb, GFP_ATOMIC);
  517. if (!skb) {
  518. macsec_txsa_put(tx_sa);
  519. return ERR_PTR(-ENOMEM);
  520. }
  521. }
  522. unprotected_len = skb->len;
  523. eth = eth_hdr(skb);
  524. sci_present = macsec_send_sci(secy);
  525. hh = skb_push(skb, macsec_extra_len(sci_present));
  526. memmove(hh, eth, 2 * ETH_ALEN);
  527. pn = tx_sa_update_pn(tx_sa, secy);
  528. if (pn.full64 == 0) {
  529. macsec_txsa_put(tx_sa);
  530. kfree_skb(skb);
  531. return ERR_PTR(-ENOLINK);
  532. }
  533. macsec_fill_sectag(hh, secy, pn.lower, sci_present);
  534. macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
  535. skb_put(skb, secy->icv_len);
  536. if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
  537. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  538. u64_stats_update_begin(&secy_stats->syncp);
  539. secy_stats->stats.OutPktsTooLong++;
  540. u64_stats_update_end(&secy_stats->syncp);
  541. macsec_txsa_put(tx_sa);
  542. kfree_skb(skb);
  543. return ERR_PTR(-EINVAL);
  544. }
  545. ret = skb_cow_data(skb, 0, &trailer);
  546. if (unlikely(ret < 0)) {
  547. macsec_txsa_put(tx_sa);
  548. kfree_skb(skb);
  549. return ERR_PTR(ret);
  550. }
  551. req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
  552. if (!req) {
  553. macsec_txsa_put(tx_sa);
  554. kfree_skb(skb);
  555. return ERR_PTR(-ENOMEM);
  556. }
  557. if (secy->xpn)
  558. macsec_fill_iv_xpn(iv, tx_sa->ssci, pn.full64, tx_sa->key.salt);
  559. else
  560. macsec_fill_iv(iv, secy->sci, pn.lower);
  561. sg_init_table(sg, ret);
  562. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  563. if (unlikely(ret < 0)) {
  564. aead_request_free(req);
  565. macsec_txsa_put(tx_sa);
  566. kfree_skb(skb);
  567. return ERR_PTR(ret);
  568. }
  569. if (tx_sc->encrypt) {
  570. int len = skb->len - macsec_hdr_len(sci_present) -
  571. secy->icv_len;
  572. aead_request_set_crypt(req, sg, sg, len, iv);
  573. aead_request_set_ad(req, macsec_hdr_len(sci_present));
  574. } else {
  575. aead_request_set_crypt(req, sg, sg, 0, iv);
  576. aead_request_set_ad(req, skb->len - secy->icv_len);
  577. }
  578. macsec_skb_cb(skb)->req = req;
  579. macsec_skb_cb(skb)->tx_sa = tx_sa;
  580. macsec_skb_cb(skb)->has_sci = sci_present;
  581. aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
  582. dev_hold(skb->dev);
  583. ret = crypto_aead_encrypt(req);
  584. if (ret == -EINPROGRESS) {
  585. return ERR_PTR(ret);
  586. } else if (ret != 0) {
  587. dev_put(skb->dev);
  588. kfree_skb(skb);
  589. aead_request_free(req);
  590. macsec_txsa_put(tx_sa);
  591. return ERR_PTR(-EINVAL);
  592. }
  593. dev_put(skb->dev);
  594. aead_request_free(req);
  595. macsec_txsa_put(tx_sa);
  596. return skb;
  597. }
  598. static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
  599. {
  600. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  601. struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
  602. struct macsec_eth_header *hdr = macsec_ethhdr(skb);
  603. u32 lowest_pn = 0;
  604. spin_lock(&rx_sa->lock);
  605. if (rx_sa->next_pn_halves.lower >= secy->replay_window)
  606. lowest_pn = rx_sa->next_pn_halves.lower - secy->replay_window;
  607. /* Now perform replay protection check again
  608. * (see IEEE 802.1AE-2006 figure 10-5)
  609. */
  610. if (secy->replay_protect && pn < lowest_pn &&
  611. (!secy->xpn || pn_same_half(pn, lowest_pn))) {
  612. spin_unlock(&rx_sa->lock);
  613. u64_stats_update_begin(&rxsc_stats->syncp);
  614. rxsc_stats->stats.InPktsLate++;
  615. u64_stats_update_end(&rxsc_stats->syncp);
  616. DEV_STATS_INC(secy->netdev, rx_dropped);
  617. return false;
  618. }
  619. if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
  620. unsigned int msdu_len = macsec_msdu_len(skb);
  621. u64_stats_update_begin(&rxsc_stats->syncp);
  622. if (hdr->tci_an & MACSEC_TCI_E)
  623. rxsc_stats->stats.InOctetsDecrypted += msdu_len;
  624. else
  625. rxsc_stats->stats.InOctetsValidated += msdu_len;
  626. u64_stats_update_end(&rxsc_stats->syncp);
  627. }
  628. if (!macsec_skb_cb(skb)->valid) {
  629. spin_unlock(&rx_sa->lock);
  630. /* 10.6.5 */
  631. if (hdr->tci_an & MACSEC_TCI_C ||
  632. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  633. u64_stats_update_begin(&rxsc_stats->syncp);
  634. rxsc_stats->stats.InPktsNotValid++;
  635. u64_stats_update_end(&rxsc_stats->syncp);
  636. this_cpu_inc(rx_sa->stats->InPktsNotValid);
  637. DEV_STATS_INC(secy->netdev, rx_errors);
  638. return false;
  639. }
  640. u64_stats_update_begin(&rxsc_stats->syncp);
  641. if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
  642. rxsc_stats->stats.InPktsInvalid++;
  643. this_cpu_inc(rx_sa->stats->InPktsInvalid);
  644. } else if (pn < lowest_pn) {
  645. rxsc_stats->stats.InPktsDelayed++;
  646. } else {
  647. rxsc_stats->stats.InPktsUnchecked++;
  648. }
  649. u64_stats_update_end(&rxsc_stats->syncp);
  650. } else {
  651. u64_stats_update_begin(&rxsc_stats->syncp);
  652. if (pn < lowest_pn) {
  653. rxsc_stats->stats.InPktsDelayed++;
  654. } else {
  655. rxsc_stats->stats.InPktsOK++;
  656. this_cpu_inc(rx_sa->stats->InPktsOK);
  657. }
  658. u64_stats_update_end(&rxsc_stats->syncp);
  659. // Instead of "pn >=" - to support pn overflow in xpn
  660. if (pn + 1 > rx_sa->next_pn_halves.lower) {
  661. rx_sa->next_pn_halves.lower = pn + 1;
  662. } else if (secy->xpn &&
  663. !pn_same_half(pn, rx_sa->next_pn_halves.lower)) {
  664. rx_sa->next_pn_halves.upper++;
  665. rx_sa->next_pn_halves.lower = pn + 1;
  666. }
  667. spin_unlock(&rx_sa->lock);
  668. }
  669. return true;
  670. }
  671. static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
  672. {
  673. skb->pkt_type = PACKET_HOST;
  674. skb->protocol = eth_type_trans(skb, dev);
  675. skb_reset_network_header(skb);
  676. if (!skb_transport_header_was_set(skb))
  677. skb_reset_transport_header(skb);
  678. skb_reset_mac_len(skb);
  679. }
  680. static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
  681. {
  682. skb->ip_summed = CHECKSUM_NONE;
  683. memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
  684. skb_pull(skb, hdr_len);
  685. pskb_trim_unique(skb, skb->len - icv_len);
  686. }
  687. static void count_rx(struct net_device *dev, int len)
  688. {
  689. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  690. u64_stats_update_begin(&stats->syncp);
  691. u64_stats_inc(&stats->rx_packets);
  692. u64_stats_add(&stats->rx_bytes, len);
  693. u64_stats_update_end(&stats->syncp);
  694. }
  695. static void macsec_decrypt_done(struct crypto_async_request *base, int err)
  696. {
  697. struct sk_buff *skb = base->data;
  698. struct net_device *dev = skb->dev;
  699. struct macsec_dev *macsec = macsec_priv(dev);
  700. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  701. struct macsec_rx_sc *rx_sc = rx_sa->sc;
  702. int len;
  703. u32 pn;
  704. aead_request_free(macsec_skb_cb(skb)->req);
  705. if (!err)
  706. macsec_skb_cb(skb)->valid = true;
  707. rcu_read_lock_bh();
  708. pn = ntohl(macsec_ethhdr(skb)->packet_number);
  709. if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
  710. rcu_read_unlock_bh();
  711. kfree_skb(skb);
  712. goto out;
  713. }
  714. macsec_finalize_skb(skb, macsec->secy.icv_len,
  715. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  716. len = skb->len;
  717. macsec_reset_skb(skb, macsec->secy.netdev);
  718. if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
  719. count_rx(dev, len);
  720. rcu_read_unlock_bh();
  721. out:
  722. macsec_rxsa_put(rx_sa);
  723. macsec_rxsc_put(rx_sc);
  724. dev_put(dev);
  725. }
  726. static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
  727. struct net_device *dev,
  728. struct macsec_rx_sa *rx_sa,
  729. sci_t sci,
  730. struct macsec_secy *secy)
  731. {
  732. int ret;
  733. struct scatterlist *sg;
  734. struct sk_buff *trailer;
  735. unsigned char *iv;
  736. struct aead_request *req;
  737. struct macsec_eth_header *hdr;
  738. u32 hdr_pn;
  739. u16 icv_len = secy->icv_len;
  740. macsec_skb_cb(skb)->valid = false;
  741. skb = skb_share_check(skb, GFP_ATOMIC);
  742. if (!skb)
  743. return ERR_PTR(-ENOMEM);
  744. ret = skb_cow_data(skb, 0, &trailer);
  745. if (unlikely(ret < 0)) {
  746. kfree_skb(skb);
  747. return ERR_PTR(ret);
  748. }
  749. req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
  750. if (!req) {
  751. kfree_skb(skb);
  752. return ERR_PTR(-ENOMEM);
  753. }
  754. hdr = (struct macsec_eth_header *)skb->data;
  755. hdr_pn = ntohl(hdr->packet_number);
  756. if (secy->xpn) {
  757. pn_t recovered_pn = rx_sa->next_pn_halves;
  758. recovered_pn.lower = hdr_pn;
  759. if (hdr_pn < rx_sa->next_pn_halves.lower &&
  760. !pn_same_half(hdr_pn, rx_sa->next_pn_halves.lower))
  761. recovered_pn.upper++;
  762. macsec_fill_iv_xpn(iv, rx_sa->ssci, recovered_pn.full64,
  763. rx_sa->key.salt);
  764. } else {
  765. macsec_fill_iv(iv, sci, hdr_pn);
  766. }
  767. sg_init_table(sg, ret);
  768. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  769. if (unlikely(ret < 0)) {
  770. aead_request_free(req);
  771. kfree_skb(skb);
  772. return ERR_PTR(ret);
  773. }
  774. if (hdr->tci_an & MACSEC_TCI_E) {
  775. /* confidentiality: ethernet + macsec header
  776. * authenticated, encrypted payload
  777. */
  778. int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
  779. aead_request_set_crypt(req, sg, sg, len, iv);
  780. aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
  781. skb = skb_unshare(skb, GFP_ATOMIC);
  782. if (!skb) {
  783. aead_request_free(req);
  784. return ERR_PTR(-ENOMEM);
  785. }
  786. } else {
  787. /* integrity only: all headers + data authenticated */
  788. aead_request_set_crypt(req, sg, sg, icv_len, iv);
  789. aead_request_set_ad(req, skb->len - icv_len);
  790. }
  791. macsec_skb_cb(skb)->req = req;
  792. skb->dev = dev;
  793. aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
  794. dev_hold(dev);
  795. ret = crypto_aead_decrypt(req);
  796. if (ret == -EINPROGRESS) {
  797. return ERR_PTR(ret);
  798. } else if (ret != 0) {
  799. /* decryption/authentication failed
  800. * 10.6 if validateFrames is disabled, deliver anyway
  801. */
  802. if (ret != -EBADMSG) {
  803. kfree_skb(skb);
  804. skb = ERR_PTR(ret);
  805. }
  806. } else {
  807. macsec_skb_cb(skb)->valid = true;
  808. }
  809. dev_put(dev);
  810. aead_request_free(req);
  811. return skb;
  812. }
  813. static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
  814. {
  815. struct macsec_rx_sc *rx_sc;
  816. for_each_rxsc(secy, rx_sc) {
  817. if (rx_sc->sci == sci)
  818. return rx_sc;
  819. }
  820. return NULL;
  821. }
  822. static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
  823. {
  824. struct macsec_rx_sc *rx_sc;
  825. for_each_rxsc_rtnl(secy, rx_sc) {
  826. if (rx_sc->sci == sci)
  827. return rx_sc;
  828. }
  829. return NULL;
  830. }
  831. static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
  832. {
  833. /* Deliver to the uncontrolled port by default */
  834. enum rx_handler_result ret = RX_HANDLER_PASS;
  835. struct ethhdr *hdr = eth_hdr(skb);
  836. struct metadata_dst *md_dst;
  837. struct macsec_rxh_data *rxd;
  838. struct macsec_dev *macsec;
  839. rcu_read_lock();
  840. rxd = macsec_data_rcu(skb->dev);
  841. md_dst = skb_metadata_dst(skb);
  842. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  843. struct sk_buff *nskb;
  844. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  845. struct net_device *ndev = macsec->secy.netdev;
  846. /* If h/w offloading is enabled, HW decodes frames and strips
  847. * the SecTAG, so we have to deduce which port to deliver to.
  848. */
  849. if (macsec_is_offloaded(macsec) && netif_running(ndev)) {
  850. if (md_dst && md_dst->type == METADATA_MACSEC &&
  851. (!find_rx_sc(&macsec->secy, md_dst->u.macsec_info.sci)))
  852. continue;
  853. if (ether_addr_equal_64bits(hdr->h_dest,
  854. ndev->dev_addr)) {
  855. /* exact match, divert skb to this port */
  856. skb->dev = ndev;
  857. skb->pkt_type = PACKET_HOST;
  858. ret = RX_HANDLER_ANOTHER;
  859. goto out;
  860. } else if (is_multicast_ether_addr_64bits(
  861. hdr->h_dest)) {
  862. /* multicast frame, deliver on this port too */
  863. nskb = skb_clone(skb, GFP_ATOMIC);
  864. if (!nskb)
  865. break;
  866. nskb->dev = ndev;
  867. if (ether_addr_equal_64bits(hdr->h_dest,
  868. ndev->broadcast))
  869. nskb->pkt_type = PACKET_BROADCAST;
  870. else
  871. nskb->pkt_type = PACKET_MULTICAST;
  872. __netif_rx(nskb);
  873. }
  874. continue;
  875. }
  876. /* 10.6 If the management control validateFrames is not
  877. * Strict, frames without a SecTAG are received, counted, and
  878. * delivered to the Controlled Port
  879. */
  880. if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  881. u64_stats_update_begin(&secy_stats->syncp);
  882. secy_stats->stats.InPktsNoTag++;
  883. u64_stats_update_end(&secy_stats->syncp);
  884. DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
  885. continue;
  886. }
  887. /* deliver on this port */
  888. nskb = skb_clone(skb, GFP_ATOMIC);
  889. if (!nskb)
  890. break;
  891. nskb->dev = ndev;
  892. if (__netif_rx(nskb) == NET_RX_SUCCESS) {
  893. u64_stats_update_begin(&secy_stats->syncp);
  894. secy_stats->stats.InPktsUntagged++;
  895. u64_stats_update_end(&secy_stats->syncp);
  896. }
  897. }
  898. out:
  899. rcu_read_unlock();
  900. return ret;
  901. }
  902. static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
  903. {
  904. struct sk_buff *skb = *pskb;
  905. struct net_device *dev = skb->dev;
  906. struct macsec_eth_header *hdr;
  907. struct macsec_secy *secy = NULL;
  908. struct macsec_rx_sc *rx_sc;
  909. struct macsec_rx_sa *rx_sa;
  910. struct macsec_rxh_data *rxd;
  911. struct macsec_dev *macsec;
  912. unsigned int len;
  913. sci_t sci;
  914. u32 hdr_pn;
  915. bool cbit;
  916. struct pcpu_rx_sc_stats *rxsc_stats;
  917. struct pcpu_secy_stats *secy_stats;
  918. bool pulled_sci;
  919. int ret;
  920. if (skb_headroom(skb) < ETH_HLEN)
  921. goto drop_direct;
  922. hdr = macsec_ethhdr(skb);
  923. if (hdr->eth.h_proto != htons(ETH_P_MACSEC))
  924. return handle_not_macsec(skb);
  925. skb = skb_unshare(skb, GFP_ATOMIC);
  926. *pskb = skb;
  927. if (!skb)
  928. return RX_HANDLER_CONSUMED;
  929. pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
  930. if (!pulled_sci) {
  931. if (!pskb_may_pull(skb, macsec_extra_len(false)))
  932. goto drop_direct;
  933. }
  934. hdr = macsec_ethhdr(skb);
  935. /* Frames with a SecTAG that has the TCI E bit set but the C
  936. * bit clear are discarded, as this reserved encoding is used
  937. * to identify frames with a SecTAG that are not to be
  938. * delivered to the Controlled Port.
  939. */
  940. if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
  941. return RX_HANDLER_PASS;
  942. /* now, pull the extra length */
  943. if (hdr->tci_an & MACSEC_TCI_SC) {
  944. if (!pulled_sci)
  945. goto drop_direct;
  946. }
  947. /* ethernet header is part of crypto processing */
  948. skb_push(skb, ETH_HLEN);
  949. macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
  950. macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
  951. sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
  952. rcu_read_lock();
  953. rxd = macsec_data_rcu(skb->dev);
  954. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  955. struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
  956. sc = sc ? macsec_rxsc_get(sc) : NULL;
  957. if (sc) {
  958. secy = &macsec->secy;
  959. rx_sc = sc;
  960. break;
  961. }
  962. }
  963. if (!secy)
  964. goto nosci;
  965. dev = secy->netdev;
  966. macsec = macsec_priv(dev);
  967. secy_stats = this_cpu_ptr(macsec->stats);
  968. rxsc_stats = this_cpu_ptr(rx_sc->stats);
  969. if (!macsec_validate_skb(skb, secy->icv_len, secy->xpn)) {
  970. u64_stats_update_begin(&secy_stats->syncp);
  971. secy_stats->stats.InPktsBadTag++;
  972. u64_stats_update_end(&secy_stats->syncp);
  973. DEV_STATS_INC(secy->netdev, rx_errors);
  974. goto drop_nosa;
  975. }
  976. rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
  977. if (!rx_sa) {
  978. /* 10.6.1 if the SA is not in use */
  979. /* If validateFrames is Strict or the C bit in the
  980. * SecTAG is set, discard
  981. */
  982. struct macsec_rx_sa *active_rx_sa = macsec_active_rxsa_get(rx_sc);
  983. if (hdr->tci_an & MACSEC_TCI_C ||
  984. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  985. u64_stats_update_begin(&rxsc_stats->syncp);
  986. rxsc_stats->stats.InPktsNotUsingSA++;
  987. u64_stats_update_end(&rxsc_stats->syncp);
  988. DEV_STATS_INC(secy->netdev, rx_errors);
  989. if (active_rx_sa)
  990. this_cpu_inc(active_rx_sa->stats->InPktsNotUsingSA);
  991. goto drop_nosa;
  992. }
  993. /* not Strict, the frame (with the SecTAG and ICV
  994. * removed) is delivered to the Controlled Port.
  995. */
  996. u64_stats_update_begin(&rxsc_stats->syncp);
  997. rxsc_stats->stats.InPktsUnusedSA++;
  998. u64_stats_update_end(&rxsc_stats->syncp);
  999. if (active_rx_sa)
  1000. this_cpu_inc(active_rx_sa->stats->InPktsUnusedSA);
  1001. goto deliver;
  1002. }
  1003. /* First, PN check to avoid decrypting obviously wrong packets */
  1004. hdr_pn = ntohl(hdr->packet_number);
  1005. if (secy->replay_protect) {
  1006. bool late;
  1007. spin_lock(&rx_sa->lock);
  1008. late = rx_sa->next_pn_halves.lower >= secy->replay_window &&
  1009. hdr_pn < (rx_sa->next_pn_halves.lower - secy->replay_window);
  1010. if (secy->xpn)
  1011. late = late && pn_same_half(rx_sa->next_pn_halves.lower, hdr_pn);
  1012. spin_unlock(&rx_sa->lock);
  1013. if (late) {
  1014. u64_stats_update_begin(&rxsc_stats->syncp);
  1015. rxsc_stats->stats.InPktsLate++;
  1016. u64_stats_update_end(&rxsc_stats->syncp);
  1017. DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
  1018. goto drop;
  1019. }
  1020. }
  1021. macsec_skb_cb(skb)->rx_sa = rx_sa;
  1022. /* Disabled && !changed text => skip validation */
  1023. if (hdr->tci_an & MACSEC_TCI_C ||
  1024. secy->validate_frames != MACSEC_VALIDATE_DISABLED)
  1025. skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
  1026. if (IS_ERR(skb)) {
  1027. /* the decrypt callback needs the reference */
  1028. if (PTR_ERR(skb) != -EINPROGRESS) {
  1029. macsec_rxsa_put(rx_sa);
  1030. macsec_rxsc_put(rx_sc);
  1031. }
  1032. rcu_read_unlock();
  1033. *pskb = NULL;
  1034. return RX_HANDLER_CONSUMED;
  1035. }
  1036. if (!macsec_post_decrypt(skb, secy, hdr_pn))
  1037. goto drop;
  1038. deliver:
  1039. macsec_finalize_skb(skb, secy->icv_len,
  1040. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1041. len = skb->len;
  1042. macsec_reset_skb(skb, secy->netdev);
  1043. if (rx_sa)
  1044. macsec_rxsa_put(rx_sa);
  1045. macsec_rxsc_put(rx_sc);
  1046. skb_orphan(skb);
  1047. ret = gro_cells_receive(&macsec->gro_cells, skb);
  1048. if (ret == NET_RX_SUCCESS)
  1049. count_rx(dev, len);
  1050. else
  1051. DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
  1052. rcu_read_unlock();
  1053. *pskb = NULL;
  1054. return RX_HANDLER_CONSUMED;
  1055. drop:
  1056. macsec_rxsa_put(rx_sa);
  1057. drop_nosa:
  1058. macsec_rxsc_put(rx_sc);
  1059. rcu_read_unlock();
  1060. drop_direct:
  1061. kfree_skb(skb);
  1062. *pskb = NULL;
  1063. return RX_HANDLER_CONSUMED;
  1064. nosci:
  1065. /* 10.6.1 if the SC is not found */
  1066. cbit = !!(hdr->tci_an & MACSEC_TCI_C);
  1067. if (!cbit)
  1068. macsec_finalize_skb(skb, MACSEC_DEFAULT_ICV_LEN,
  1069. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1070. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  1071. struct sk_buff *nskb;
  1072. secy_stats = this_cpu_ptr(macsec->stats);
  1073. /* If validateFrames is Strict or the C bit in the
  1074. * SecTAG is set, discard
  1075. */
  1076. if (cbit ||
  1077. macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  1078. u64_stats_update_begin(&secy_stats->syncp);
  1079. secy_stats->stats.InPktsNoSCI++;
  1080. u64_stats_update_end(&secy_stats->syncp);
  1081. DEV_STATS_INC(macsec->secy.netdev, rx_errors);
  1082. continue;
  1083. }
  1084. /* not strict, the frame (with the SecTAG and ICV
  1085. * removed) is delivered to the Controlled Port.
  1086. */
  1087. nskb = skb_clone(skb, GFP_ATOMIC);
  1088. if (!nskb)
  1089. break;
  1090. macsec_reset_skb(nskb, macsec->secy.netdev);
  1091. ret = __netif_rx(nskb);
  1092. if (ret == NET_RX_SUCCESS) {
  1093. u64_stats_update_begin(&secy_stats->syncp);
  1094. secy_stats->stats.InPktsUnknownSCI++;
  1095. u64_stats_update_end(&secy_stats->syncp);
  1096. } else {
  1097. DEV_STATS_INC(macsec->secy.netdev, rx_dropped);
  1098. }
  1099. }
  1100. rcu_read_unlock();
  1101. *pskb = skb;
  1102. return RX_HANDLER_PASS;
  1103. }
  1104. static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
  1105. {
  1106. struct crypto_aead *tfm;
  1107. int ret;
  1108. tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  1109. if (IS_ERR(tfm))
  1110. return tfm;
  1111. ret = crypto_aead_setkey(tfm, key, key_len);
  1112. if (ret < 0)
  1113. goto fail;
  1114. ret = crypto_aead_setauthsize(tfm, icv_len);
  1115. if (ret < 0)
  1116. goto fail;
  1117. return tfm;
  1118. fail:
  1119. crypto_free_aead(tfm);
  1120. return ERR_PTR(ret);
  1121. }
  1122. static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
  1123. int icv_len)
  1124. {
  1125. rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
  1126. if (!rx_sa->stats)
  1127. return -ENOMEM;
  1128. rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1129. if (IS_ERR(rx_sa->key.tfm)) {
  1130. free_percpu(rx_sa->stats);
  1131. return PTR_ERR(rx_sa->key.tfm);
  1132. }
  1133. rx_sa->ssci = MACSEC_UNDEF_SSCI;
  1134. rx_sa->active = false;
  1135. rx_sa->next_pn = 1;
  1136. refcount_set(&rx_sa->refcnt, 1);
  1137. spin_lock_init(&rx_sa->lock);
  1138. return 0;
  1139. }
  1140. static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
  1141. {
  1142. rx_sa->active = false;
  1143. macsec_rxsa_put(rx_sa);
  1144. }
  1145. static void free_rx_sc(struct macsec_rx_sc *rx_sc)
  1146. {
  1147. int i;
  1148. for (i = 0; i < MACSEC_NUM_AN; i++) {
  1149. struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
  1150. RCU_INIT_POINTER(rx_sc->sa[i], NULL);
  1151. if (sa)
  1152. clear_rx_sa(sa);
  1153. }
  1154. macsec_rxsc_put(rx_sc);
  1155. }
  1156. static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
  1157. {
  1158. struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
  1159. for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
  1160. rx_sc;
  1161. rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
  1162. if (rx_sc->sci == sci) {
  1163. if (rx_sc->active)
  1164. secy->n_rx_sc--;
  1165. rcu_assign_pointer(*rx_scp, rx_sc->next);
  1166. return rx_sc;
  1167. }
  1168. }
  1169. return NULL;
  1170. }
  1171. static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci,
  1172. bool active)
  1173. {
  1174. struct macsec_rx_sc *rx_sc;
  1175. struct macsec_dev *macsec;
  1176. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  1177. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  1178. struct macsec_secy *secy;
  1179. list_for_each_entry(macsec, &rxd->secys, secys) {
  1180. if (find_rx_sc_rtnl(&macsec->secy, sci))
  1181. return ERR_PTR(-EEXIST);
  1182. }
  1183. rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
  1184. if (!rx_sc)
  1185. return ERR_PTR(-ENOMEM);
  1186. rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
  1187. if (!rx_sc->stats) {
  1188. kfree(rx_sc);
  1189. return ERR_PTR(-ENOMEM);
  1190. }
  1191. rx_sc->sci = sci;
  1192. rx_sc->active = active;
  1193. refcount_set(&rx_sc->refcnt, 1);
  1194. secy = &macsec_priv(dev)->secy;
  1195. rcu_assign_pointer(rx_sc->next, secy->rx_sc);
  1196. rcu_assign_pointer(secy->rx_sc, rx_sc);
  1197. if (rx_sc->active)
  1198. secy->n_rx_sc++;
  1199. return rx_sc;
  1200. }
  1201. static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
  1202. int icv_len)
  1203. {
  1204. tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
  1205. if (!tx_sa->stats)
  1206. return -ENOMEM;
  1207. tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1208. if (IS_ERR(tx_sa->key.tfm)) {
  1209. free_percpu(tx_sa->stats);
  1210. return PTR_ERR(tx_sa->key.tfm);
  1211. }
  1212. tx_sa->ssci = MACSEC_UNDEF_SSCI;
  1213. tx_sa->active = false;
  1214. refcount_set(&tx_sa->refcnt, 1);
  1215. spin_lock_init(&tx_sa->lock);
  1216. return 0;
  1217. }
  1218. static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
  1219. {
  1220. tx_sa->active = false;
  1221. macsec_txsa_put(tx_sa);
  1222. }
  1223. static struct genl_family macsec_fam;
  1224. static struct net_device *get_dev_from_nl(struct net *net,
  1225. struct nlattr **attrs)
  1226. {
  1227. int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
  1228. struct net_device *dev;
  1229. dev = __dev_get_by_index(net, ifindex);
  1230. if (!dev)
  1231. return ERR_PTR(-ENODEV);
  1232. if (!netif_is_macsec(dev))
  1233. return ERR_PTR(-ENODEV);
  1234. return dev;
  1235. }
  1236. static enum macsec_offload nla_get_offload(const struct nlattr *nla)
  1237. {
  1238. return (__force enum macsec_offload)nla_get_u8(nla);
  1239. }
  1240. static sci_t nla_get_sci(const struct nlattr *nla)
  1241. {
  1242. return (__force sci_t)nla_get_u64(nla);
  1243. }
  1244. static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
  1245. int padattr)
  1246. {
  1247. return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
  1248. }
  1249. static ssci_t nla_get_ssci(const struct nlattr *nla)
  1250. {
  1251. return (__force ssci_t)nla_get_u32(nla);
  1252. }
  1253. static int nla_put_ssci(struct sk_buff *skb, int attrtype, ssci_t value)
  1254. {
  1255. return nla_put_u32(skb, attrtype, (__force u64)value);
  1256. }
  1257. static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
  1258. struct nlattr **attrs,
  1259. struct nlattr **tb_sa,
  1260. struct net_device **devp,
  1261. struct macsec_secy **secyp,
  1262. struct macsec_tx_sc **scp,
  1263. u8 *assoc_num)
  1264. {
  1265. struct net_device *dev;
  1266. struct macsec_secy *secy;
  1267. struct macsec_tx_sc *tx_sc;
  1268. struct macsec_tx_sa *tx_sa;
  1269. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1270. return ERR_PTR(-EINVAL);
  1271. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1272. dev = get_dev_from_nl(net, attrs);
  1273. if (IS_ERR(dev))
  1274. return ERR_CAST(dev);
  1275. if (*assoc_num >= MACSEC_NUM_AN)
  1276. return ERR_PTR(-EINVAL);
  1277. secy = &macsec_priv(dev)->secy;
  1278. tx_sc = &secy->tx_sc;
  1279. tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
  1280. if (!tx_sa)
  1281. return ERR_PTR(-ENODEV);
  1282. *devp = dev;
  1283. *scp = tx_sc;
  1284. *secyp = secy;
  1285. return tx_sa;
  1286. }
  1287. static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
  1288. struct nlattr **attrs,
  1289. struct nlattr **tb_rxsc,
  1290. struct net_device **devp,
  1291. struct macsec_secy **secyp)
  1292. {
  1293. struct net_device *dev;
  1294. struct macsec_secy *secy;
  1295. struct macsec_rx_sc *rx_sc;
  1296. sci_t sci;
  1297. dev = get_dev_from_nl(net, attrs);
  1298. if (IS_ERR(dev))
  1299. return ERR_CAST(dev);
  1300. secy = &macsec_priv(dev)->secy;
  1301. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1302. return ERR_PTR(-EINVAL);
  1303. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1304. rx_sc = find_rx_sc_rtnl(secy, sci);
  1305. if (!rx_sc)
  1306. return ERR_PTR(-ENODEV);
  1307. *secyp = secy;
  1308. *devp = dev;
  1309. return rx_sc;
  1310. }
  1311. static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
  1312. struct nlattr **attrs,
  1313. struct nlattr **tb_rxsc,
  1314. struct nlattr **tb_sa,
  1315. struct net_device **devp,
  1316. struct macsec_secy **secyp,
  1317. struct macsec_rx_sc **scp,
  1318. u8 *assoc_num)
  1319. {
  1320. struct macsec_rx_sc *rx_sc;
  1321. struct macsec_rx_sa *rx_sa;
  1322. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1323. return ERR_PTR(-EINVAL);
  1324. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1325. if (*assoc_num >= MACSEC_NUM_AN)
  1326. return ERR_PTR(-EINVAL);
  1327. rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
  1328. if (IS_ERR(rx_sc))
  1329. return ERR_CAST(rx_sc);
  1330. rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
  1331. if (!rx_sa)
  1332. return ERR_PTR(-ENODEV);
  1333. *scp = rx_sc;
  1334. return rx_sa;
  1335. }
  1336. static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
  1337. [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
  1338. [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
  1339. [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
  1340. [MACSEC_ATTR_OFFLOAD] = { .type = NLA_NESTED },
  1341. };
  1342. static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
  1343. [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
  1344. [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
  1345. };
  1346. static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
  1347. [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
  1348. [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
  1349. [MACSEC_SA_ATTR_PN] = NLA_POLICY_MIN_LEN(4),
  1350. [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
  1351. .len = MACSEC_KEYID_LEN, },
  1352. [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
  1353. .len = MACSEC_MAX_KEY_LEN, },
  1354. [MACSEC_SA_ATTR_SSCI] = { .type = NLA_U32 },
  1355. [MACSEC_SA_ATTR_SALT] = { .type = NLA_BINARY,
  1356. .len = MACSEC_SALT_LEN, },
  1357. };
  1358. static const struct nla_policy macsec_genl_offload_policy[NUM_MACSEC_OFFLOAD_ATTR] = {
  1359. [MACSEC_OFFLOAD_ATTR_TYPE] = { .type = NLA_U8 },
  1360. };
  1361. /* Offloads an operation to a device driver */
  1362. static int macsec_offload(int (* const func)(struct macsec_context *),
  1363. struct macsec_context *ctx)
  1364. {
  1365. int ret;
  1366. if (unlikely(!func))
  1367. return 0;
  1368. if (ctx->offload == MACSEC_OFFLOAD_PHY)
  1369. mutex_lock(&ctx->phydev->lock);
  1370. ret = (*func)(ctx);
  1371. if (ctx->offload == MACSEC_OFFLOAD_PHY)
  1372. mutex_unlock(&ctx->phydev->lock);
  1373. return ret;
  1374. }
  1375. static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
  1376. {
  1377. if (!attrs[MACSEC_ATTR_SA_CONFIG])
  1378. return -EINVAL;
  1379. if (nla_parse_nested_deprecated(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG], macsec_genl_sa_policy, NULL))
  1380. return -EINVAL;
  1381. return 0;
  1382. }
  1383. static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
  1384. {
  1385. if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
  1386. return -EINVAL;
  1387. if (nla_parse_nested_deprecated(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG], macsec_genl_rxsc_policy, NULL))
  1388. return -EINVAL;
  1389. return 0;
  1390. }
  1391. static bool validate_add_rxsa(struct nlattr **attrs)
  1392. {
  1393. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1394. !attrs[MACSEC_SA_ATTR_KEY] ||
  1395. !attrs[MACSEC_SA_ATTR_KEYID])
  1396. return false;
  1397. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1398. return false;
  1399. if (attrs[MACSEC_SA_ATTR_PN] &&
  1400. nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1401. return false;
  1402. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1403. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1404. return false;
  1405. }
  1406. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1407. return false;
  1408. return true;
  1409. }
  1410. static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
  1411. {
  1412. struct net_device *dev;
  1413. struct nlattr **attrs = info->attrs;
  1414. struct macsec_secy *secy;
  1415. struct macsec_rx_sc *rx_sc;
  1416. struct macsec_rx_sa *rx_sa;
  1417. unsigned char assoc_num;
  1418. int pn_len;
  1419. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1420. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1421. int err;
  1422. if (!attrs[MACSEC_ATTR_IFINDEX])
  1423. return -EINVAL;
  1424. if (parse_sa_config(attrs, tb_sa))
  1425. return -EINVAL;
  1426. if (parse_rxsc_config(attrs, tb_rxsc))
  1427. return -EINVAL;
  1428. if (!validate_add_rxsa(tb_sa))
  1429. return -EINVAL;
  1430. rtnl_lock();
  1431. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1432. if (IS_ERR(rx_sc)) {
  1433. rtnl_unlock();
  1434. return PTR_ERR(rx_sc);
  1435. }
  1436. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1437. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1438. pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
  1439. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1440. rtnl_unlock();
  1441. return -EINVAL;
  1442. }
  1443. pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
  1444. if (tb_sa[MACSEC_SA_ATTR_PN] &&
  1445. nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
  1446. pr_notice("macsec: nl: add_rxsa: bad pn length: %d != %d\n",
  1447. nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
  1448. rtnl_unlock();
  1449. return -EINVAL;
  1450. }
  1451. if (secy->xpn) {
  1452. if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
  1453. rtnl_unlock();
  1454. return -EINVAL;
  1455. }
  1456. if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
  1457. pr_notice("macsec: nl: add_rxsa: bad salt length: %d != %d\n",
  1458. nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
  1459. MACSEC_SALT_LEN);
  1460. rtnl_unlock();
  1461. return -EINVAL;
  1462. }
  1463. }
  1464. rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
  1465. if (rx_sa) {
  1466. rtnl_unlock();
  1467. return -EBUSY;
  1468. }
  1469. rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
  1470. if (!rx_sa) {
  1471. rtnl_unlock();
  1472. return -ENOMEM;
  1473. }
  1474. err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1475. secy->key_len, secy->icv_len);
  1476. if (err < 0) {
  1477. kfree(rx_sa);
  1478. rtnl_unlock();
  1479. return err;
  1480. }
  1481. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1482. spin_lock_bh(&rx_sa->lock);
  1483. rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
  1484. spin_unlock_bh(&rx_sa->lock);
  1485. }
  1486. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1487. rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1488. rx_sa->sc = rx_sc;
  1489. if (secy->xpn) {
  1490. rx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
  1491. nla_memcpy(rx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
  1492. MACSEC_SALT_LEN);
  1493. }
  1494. /* If h/w offloading is available, propagate to the device */
  1495. if (macsec_is_offloaded(netdev_priv(dev))) {
  1496. const struct macsec_ops *ops;
  1497. struct macsec_context ctx;
  1498. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1499. if (!ops) {
  1500. err = -EOPNOTSUPP;
  1501. goto cleanup;
  1502. }
  1503. ctx.sa.assoc_num = assoc_num;
  1504. ctx.sa.rx_sa = rx_sa;
  1505. ctx.secy = secy;
  1506. memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1507. secy->key_len);
  1508. err = macsec_offload(ops->mdo_add_rxsa, &ctx);
  1509. memzero_explicit(ctx.sa.key, secy->key_len);
  1510. if (err)
  1511. goto cleanup;
  1512. }
  1513. nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1514. rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
  1515. rtnl_unlock();
  1516. return 0;
  1517. cleanup:
  1518. macsec_rxsa_put(rx_sa);
  1519. rtnl_unlock();
  1520. return err;
  1521. }
  1522. static bool validate_add_rxsc(struct nlattr **attrs)
  1523. {
  1524. if (!attrs[MACSEC_RXSC_ATTR_SCI])
  1525. return false;
  1526. if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
  1527. if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
  1528. return false;
  1529. }
  1530. return true;
  1531. }
  1532. static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
  1533. {
  1534. struct net_device *dev;
  1535. sci_t sci = MACSEC_UNDEF_SCI;
  1536. struct nlattr **attrs = info->attrs;
  1537. struct macsec_rx_sc *rx_sc;
  1538. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1539. struct macsec_secy *secy;
  1540. bool active = true;
  1541. int ret;
  1542. if (!attrs[MACSEC_ATTR_IFINDEX])
  1543. return -EINVAL;
  1544. if (parse_rxsc_config(attrs, tb_rxsc))
  1545. return -EINVAL;
  1546. if (!validate_add_rxsc(tb_rxsc))
  1547. return -EINVAL;
  1548. rtnl_lock();
  1549. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1550. if (IS_ERR(dev)) {
  1551. rtnl_unlock();
  1552. return PTR_ERR(dev);
  1553. }
  1554. secy = &macsec_priv(dev)->secy;
  1555. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1556. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
  1557. active = nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1558. rx_sc = create_rx_sc(dev, sci, active);
  1559. if (IS_ERR(rx_sc)) {
  1560. rtnl_unlock();
  1561. return PTR_ERR(rx_sc);
  1562. }
  1563. if (macsec_is_offloaded(netdev_priv(dev))) {
  1564. const struct macsec_ops *ops;
  1565. struct macsec_context ctx;
  1566. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1567. if (!ops) {
  1568. ret = -EOPNOTSUPP;
  1569. goto cleanup;
  1570. }
  1571. ctx.rx_sc = rx_sc;
  1572. ctx.secy = secy;
  1573. ret = macsec_offload(ops->mdo_add_rxsc, &ctx);
  1574. if (ret)
  1575. goto cleanup;
  1576. }
  1577. rtnl_unlock();
  1578. return 0;
  1579. cleanup:
  1580. del_rx_sc(secy, sci);
  1581. free_rx_sc(rx_sc);
  1582. rtnl_unlock();
  1583. return ret;
  1584. }
  1585. static bool validate_add_txsa(struct nlattr **attrs)
  1586. {
  1587. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1588. !attrs[MACSEC_SA_ATTR_PN] ||
  1589. !attrs[MACSEC_SA_ATTR_KEY] ||
  1590. !attrs[MACSEC_SA_ATTR_KEYID])
  1591. return false;
  1592. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1593. return false;
  1594. if (nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1595. return false;
  1596. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1597. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1598. return false;
  1599. }
  1600. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1601. return false;
  1602. return true;
  1603. }
  1604. static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
  1605. {
  1606. struct net_device *dev;
  1607. struct nlattr **attrs = info->attrs;
  1608. struct macsec_secy *secy;
  1609. struct macsec_tx_sc *tx_sc;
  1610. struct macsec_tx_sa *tx_sa;
  1611. unsigned char assoc_num;
  1612. int pn_len;
  1613. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1614. bool was_operational;
  1615. int err;
  1616. if (!attrs[MACSEC_ATTR_IFINDEX])
  1617. return -EINVAL;
  1618. if (parse_sa_config(attrs, tb_sa))
  1619. return -EINVAL;
  1620. if (!validate_add_txsa(tb_sa))
  1621. return -EINVAL;
  1622. rtnl_lock();
  1623. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1624. if (IS_ERR(dev)) {
  1625. rtnl_unlock();
  1626. return PTR_ERR(dev);
  1627. }
  1628. secy = &macsec_priv(dev)->secy;
  1629. tx_sc = &secy->tx_sc;
  1630. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1631. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1632. pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
  1633. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1634. rtnl_unlock();
  1635. return -EINVAL;
  1636. }
  1637. pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
  1638. if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
  1639. pr_notice("macsec: nl: add_txsa: bad pn length: %d != %d\n",
  1640. nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
  1641. rtnl_unlock();
  1642. return -EINVAL;
  1643. }
  1644. if (secy->xpn) {
  1645. if (!tb_sa[MACSEC_SA_ATTR_SSCI] || !tb_sa[MACSEC_SA_ATTR_SALT]) {
  1646. rtnl_unlock();
  1647. return -EINVAL;
  1648. }
  1649. if (nla_len(tb_sa[MACSEC_SA_ATTR_SALT]) != MACSEC_SALT_LEN) {
  1650. pr_notice("macsec: nl: add_txsa: bad salt length: %d != %d\n",
  1651. nla_len(tb_sa[MACSEC_SA_ATTR_SALT]),
  1652. MACSEC_SALT_LEN);
  1653. rtnl_unlock();
  1654. return -EINVAL;
  1655. }
  1656. }
  1657. tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
  1658. if (tx_sa) {
  1659. rtnl_unlock();
  1660. return -EBUSY;
  1661. }
  1662. tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
  1663. if (!tx_sa) {
  1664. rtnl_unlock();
  1665. return -ENOMEM;
  1666. }
  1667. err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1668. secy->key_len, secy->icv_len);
  1669. if (err < 0) {
  1670. kfree(tx_sa);
  1671. rtnl_unlock();
  1672. return err;
  1673. }
  1674. spin_lock_bh(&tx_sa->lock);
  1675. tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
  1676. spin_unlock_bh(&tx_sa->lock);
  1677. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1678. tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1679. was_operational = secy->operational;
  1680. if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
  1681. secy->operational = true;
  1682. if (secy->xpn) {
  1683. tx_sa->ssci = nla_get_ssci(tb_sa[MACSEC_SA_ATTR_SSCI]);
  1684. nla_memcpy(tx_sa->key.salt.bytes, tb_sa[MACSEC_SA_ATTR_SALT],
  1685. MACSEC_SALT_LEN);
  1686. }
  1687. /* If h/w offloading is available, propagate to the device */
  1688. if (macsec_is_offloaded(netdev_priv(dev))) {
  1689. const struct macsec_ops *ops;
  1690. struct macsec_context ctx;
  1691. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1692. if (!ops) {
  1693. err = -EOPNOTSUPP;
  1694. goto cleanup;
  1695. }
  1696. ctx.sa.assoc_num = assoc_num;
  1697. ctx.sa.tx_sa = tx_sa;
  1698. ctx.secy = secy;
  1699. memcpy(ctx.sa.key, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1700. secy->key_len);
  1701. err = macsec_offload(ops->mdo_add_txsa, &ctx);
  1702. memzero_explicit(ctx.sa.key, secy->key_len);
  1703. if (err)
  1704. goto cleanup;
  1705. }
  1706. nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1707. rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
  1708. rtnl_unlock();
  1709. return 0;
  1710. cleanup:
  1711. secy->operational = was_operational;
  1712. macsec_txsa_put(tx_sa);
  1713. rtnl_unlock();
  1714. return err;
  1715. }
  1716. static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
  1717. {
  1718. struct nlattr **attrs = info->attrs;
  1719. struct net_device *dev;
  1720. struct macsec_secy *secy;
  1721. struct macsec_rx_sc *rx_sc;
  1722. struct macsec_rx_sa *rx_sa;
  1723. u8 assoc_num;
  1724. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1725. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1726. int ret;
  1727. if (!attrs[MACSEC_ATTR_IFINDEX])
  1728. return -EINVAL;
  1729. if (parse_sa_config(attrs, tb_sa))
  1730. return -EINVAL;
  1731. if (parse_rxsc_config(attrs, tb_rxsc))
  1732. return -EINVAL;
  1733. rtnl_lock();
  1734. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1735. &dev, &secy, &rx_sc, &assoc_num);
  1736. if (IS_ERR(rx_sa)) {
  1737. rtnl_unlock();
  1738. return PTR_ERR(rx_sa);
  1739. }
  1740. if (rx_sa->active) {
  1741. rtnl_unlock();
  1742. return -EBUSY;
  1743. }
  1744. /* If h/w offloading is available, propagate to the device */
  1745. if (macsec_is_offloaded(netdev_priv(dev))) {
  1746. const struct macsec_ops *ops;
  1747. struct macsec_context ctx;
  1748. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1749. if (!ops) {
  1750. ret = -EOPNOTSUPP;
  1751. goto cleanup;
  1752. }
  1753. ctx.sa.assoc_num = assoc_num;
  1754. ctx.sa.rx_sa = rx_sa;
  1755. ctx.secy = secy;
  1756. ret = macsec_offload(ops->mdo_del_rxsa, &ctx);
  1757. if (ret)
  1758. goto cleanup;
  1759. }
  1760. RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
  1761. clear_rx_sa(rx_sa);
  1762. rtnl_unlock();
  1763. return 0;
  1764. cleanup:
  1765. rtnl_unlock();
  1766. return ret;
  1767. }
  1768. static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
  1769. {
  1770. struct nlattr **attrs = info->attrs;
  1771. struct net_device *dev;
  1772. struct macsec_secy *secy;
  1773. struct macsec_rx_sc *rx_sc;
  1774. sci_t sci;
  1775. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1776. int ret;
  1777. if (!attrs[MACSEC_ATTR_IFINDEX])
  1778. return -EINVAL;
  1779. if (parse_rxsc_config(attrs, tb_rxsc))
  1780. return -EINVAL;
  1781. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1782. return -EINVAL;
  1783. rtnl_lock();
  1784. dev = get_dev_from_nl(genl_info_net(info), info->attrs);
  1785. if (IS_ERR(dev)) {
  1786. rtnl_unlock();
  1787. return PTR_ERR(dev);
  1788. }
  1789. secy = &macsec_priv(dev)->secy;
  1790. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1791. rx_sc = del_rx_sc(secy, sci);
  1792. if (!rx_sc) {
  1793. rtnl_unlock();
  1794. return -ENODEV;
  1795. }
  1796. /* If h/w offloading is available, propagate to the device */
  1797. if (macsec_is_offloaded(netdev_priv(dev))) {
  1798. const struct macsec_ops *ops;
  1799. struct macsec_context ctx;
  1800. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1801. if (!ops) {
  1802. ret = -EOPNOTSUPP;
  1803. goto cleanup;
  1804. }
  1805. ctx.rx_sc = rx_sc;
  1806. ctx.secy = secy;
  1807. ret = macsec_offload(ops->mdo_del_rxsc, &ctx);
  1808. if (ret)
  1809. goto cleanup;
  1810. }
  1811. free_rx_sc(rx_sc);
  1812. rtnl_unlock();
  1813. return 0;
  1814. cleanup:
  1815. rtnl_unlock();
  1816. return ret;
  1817. }
  1818. static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
  1819. {
  1820. struct nlattr **attrs = info->attrs;
  1821. struct net_device *dev;
  1822. struct macsec_secy *secy;
  1823. struct macsec_tx_sc *tx_sc;
  1824. struct macsec_tx_sa *tx_sa;
  1825. u8 assoc_num;
  1826. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1827. int ret;
  1828. if (!attrs[MACSEC_ATTR_IFINDEX])
  1829. return -EINVAL;
  1830. if (parse_sa_config(attrs, tb_sa))
  1831. return -EINVAL;
  1832. rtnl_lock();
  1833. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1834. &dev, &secy, &tx_sc, &assoc_num);
  1835. if (IS_ERR(tx_sa)) {
  1836. rtnl_unlock();
  1837. return PTR_ERR(tx_sa);
  1838. }
  1839. if (tx_sa->active) {
  1840. rtnl_unlock();
  1841. return -EBUSY;
  1842. }
  1843. /* If h/w offloading is available, propagate to the device */
  1844. if (macsec_is_offloaded(netdev_priv(dev))) {
  1845. const struct macsec_ops *ops;
  1846. struct macsec_context ctx;
  1847. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1848. if (!ops) {
  1849. ret = -EOPNOTSUPP;
  1850. goto cleanup;
  1851. }
  1852. ctx.sa.assoc_num = assoc_num;
  1853. ctx.sa.tx_sa = tx_sa;
  1854. ctx.secy = secy;
  1855. ret = macsec_offload(ops->mdo_del_txsa, &ctx);
  1856. if (ret)
  1857. goto cleanup;
  1858. }
  1859. RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
  1860. clear_tx_sa(tx_sa);
  1861. rtnl_unlock();
  1862. return 0;
  1863. cleanup:
  1864. rtnl_unlock();
  1865. return ret;
  1866. }
  1867. static bool validate_upd_sa(struct nlattr **attrs)
  1868. {
  1869. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1870. attrs[MACSEC_SA_ATTR_KEY] ||
  1871. attrs[MACSEC_SA_ATTR_KEYID] ||
  1872. attrs[MACSEC_SA_ATTR_SSCI] ||
  1873. attrs[MACSEC_SA_ATTR_SALT])
  1874. return false;
  1875. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1876. return false;
  1877. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u64(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1878. return false;
  1879. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1880. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1881. return false;
  1882. }
  1883. return true;
  1884. }
  1885. static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
  1886. {
  1887. struct nlattr **attrs = info->attrs;
  1888. struct net_device *dev;
  1889. struct macsec_secy *secy;
  1890. struct macsec_tx_sc *tx_sc;
  1891. struct macsec_tx_sa *tx_sa;
  1892. u8 assoc_num;
  1893. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1894. bool was_operational, was_active;
  1895. pn_t prev_pn;
  1896. int ret = 0;
  1897. prev_pn.full64 = 0;
  1898. if (!attrs[MACSEC_ATTR_IFINDEX])
  1899. return -EINVAL;
  1900. if (parse_sa_config(attrs, tb_sa))
  1901. return -EINVAL;
  1902. if (!validate_upd_sa(tb_sa))
  1903. return -EINVAL;
  1904. rtnl_lock();
  1905. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1906. &dev, &secy, &tx_sc, &assoc_num);
  1907. if (IS_ERR(tx_sa)) {
  1908. rtnl_unlock();
  1909. return PTR_ERR(tx_sa);
  1910. }
  1911. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1912. int pn_len;
  1913. pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
  1914. if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
  1915. pr_notice("macsec: nl: upd_txsa: bad pn length: %d != %d\n",
  1916. nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
  1917. rtnl_unlock();
  1918. return -EINVAL;
  1919. }
  1920. spin_lock_bh(&tx_sa->lock);
  1921. prev_pn = tx_sa->next_pn_halves;
  1922. tx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
  1923. spin_unlock_bh(&tx_sa->lock);
  1924. }
  1925. was_active = tx_sa->active;
  1926. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1927. tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1928. was_operational = secy->operational;
  1929. if (assoc_num == tx_sc->encoding_sa)
  1930. secy->operational = tx_sa->active;
  1931. /* If h/w offloading is available, propagate to the device */
  1932. if (macsec_is_offloaded(netdev_priv(dev))) {
  1933. const struct macsec_ops *ops;
  1934. struct macsec_context ctx;
  1935. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  1936. if (!ops) {
  1937. ret = -EOPNOTSUPP;
  1938. goto cleanup;
  1939. }
  1940. ctx.sa.assoc_num = assoc_num;
  1941. ctx.sa.tx_sa = tx_sa;
  1942. ctx.secy = secy;
  1943. ret = macsec_offload(ops->mdo_upd_txsa, &ctx);
  1944. if (ret)
  1945. goto cleanup;
  1946. }
  1947. rtnl_unlock();
  1948. return 0;
  1949. cleanup:
  1950. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1951. spin_lock_bh(&tx_sa->lock);
  1952. tx_sa->next_pn_halves = prev_pn;
  1953. spin_unlock_bh(&tx_sa->lock);
  1954. }
  1955. tx_sa->active = was_active;
  1956. secy->operational = was_operational;
  1957. rtnl_unlock();
  1958. return ret;
  1959. }
  1960. static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
  1961. {
  1962. struct nlattr **attrs = info->attrs;
  1963. struct net_device *dev;
  1964. struct macsec_secy *secy;
  1965. struct macsec_rx_sc *rx_sc;
  1966. struct macsec_rx_sa *rx_sa;
  1967. u8 assoc_num;
  1968. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1969. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1970. bool was_active;
  1971. pn_t prev_pn;
  1972. int ret = 0;
  1973. prev_pn.full64 = 0;
  1974. if (!attrs[MACSEC_ATTR_IFINDEX])
  1975. return -EINVAL;
  1976. if (parse_rxsc_config(attrs, tb_rxsc))
  1977. return -EINVAL;
  1978. if (parse_sa_config(attrs, tb_sa))
  1979. return -EINVAL;
  1980. if (!validate_upd_sa(tb_sa))
  1981. return -EINVAL;
  1982. rtnl_lock();
  1983. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1984. &dev, &secy, &rx_sc, &assoc_num);
  1985. if (IS_ERR(rx_sa)) {
  1986. rtnl_unlock();
  1987. return PTR_ERR(rx_sa);
  1988. }
  1989. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1990. int pn_len;
  1991. pn_len = secy->xpn ? MACSEC_XPN_PN_LEN : MACSEC_DEFAULT_PN_LEN;
  1992. if (nla_len(tb_sa[MACSEC_SA_ATTR_PN]) != pn_len) {
  1993. pr_notice("macsec: nl: upd_rxsa: bad pn length: %d != %d\n",
  1994. nla_len(tb_sa[MACSEC_SA_ATTR_PN]), pn_len);
  1995. rtnl_unlock();
  1996. return -EINVAL;
  1997. }
  1998. spin_lock_bh(&rx_sa->lock);
  1999. prev_pn = rx_sa->next_pn_halves;
  2000. rx_sa->next_pn = nla_get_u64(tb_sa[MACSEC_SA_ATTR_PN]);
  2001. spin_unlock_bh(&rx_sa->lock);
  2002. }
  2003. was_active = rx_sa->active;
  2004. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  2005. rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  2006. /* If h/w offloading is available, propagate to the device */
  2007. if (macsec_is_offloaded(netdev_priv(dev))) {
  2008. const struct macsec_ops *ops;
  2009. struct macsec_context ctx;
  2010. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  2011. if (!ops) {
  2012. ret = -EOPNOTSUPP;
  2013. goto cleanup;
  2014. }
  2015. ctx.sa.assoc_num = assoc_num;
  2016. ctx.sa.rx_sa = rx_sa;
  2017. ctx.secy = secy;
  2018. ret = macsec_offload(ops->mdo_upd_rxsa, &ctx);
  2019. if (ret)
  2020. goto cleanup;
  2021. }
  2022. rtnl_unlock();
  2023. return 0;
  2024. cleanup:
  2025. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  2026. spin_lock_bh(&rx_sa->lock);
  2027. rx_sa->next_pn_halves = prev_pn;
  2028. spin_unlock_bh(&rx_sa->lock);
  2029. }
  2030. rx_sa->active = was_active;
  2031. rtnl_unlock();
  2032. return ret;
  2033. }
  2034. static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
  2035. {
  2036. struct nlattr **attrs = info->attrs;
  2037. struct net_device *dev;
  2038. struct macsec_secy *secy;
  2039. struct macsec_rx_sc *rx_sc;
  2040. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  2041. unsigned int prev_n_rx_sc;
  2042. bool was_active;
  2043. int ret;
  2044. if (!attrs[MACSEC_ATTR_IFINDEX])
  2045. return -EINVAL;
  2046. if (parse_rxsc_config(attrs, tb_rxsc))
  2047. return -EINVAL;
  2048. if (!validate_add_rxsc(tb_rxsc))
  2049. return -EINVAL;
  2050. rtnl_lock();
  2051. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  2052. if (IS_ERR(rx_sc)) {
  2053. rtnl_unlock();
  2054. return PTR_ERR(rx_sc);
  2055. }
  2056. was_active = rx_sc->active;
  2057. prev_n_rx_sc = secy->n_rx_sc;
  2058. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
  2059. bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  2060. if (rx_sc->active != new)
  2061. secy->n_rx_sc += new ? 1 : -1;
  2062. rx_sc->active = new;
  2063. }
  2064. /* If h/w offloading is available, propagate to the device */
  2065. if (macsec_is_offloaded(netdev_priv(dev))) {
  2066. const struct macsec_ops *ops;
  2067. struct macsec_context ctx;
  2068. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  2069. if (!ops) {
  2070. ret = -EOPNOTSUPP;
  2071. goto cleanup;
  2072. }
  2073. ctx.rx_sc = rx_sc;
  2074. ctx.secy = secy;
  2075. ret = macsec_offload(ops->mdo_upd_rxsc, &ctx);
  2076. if (ret)
  2077. goto cleanup;
  2078. }
  2079. rtnl_unlock();
  2080. return 0;
  2081. cleanup:
  2082. secy->n_rx_sc = prev_n_rx_sc;
  2083. rx_sc->active = was_active;
  2084. rtnl_unlock();
  2085. return ret;
  2086. }
  2087. static bool macsec_is_configured(struct macsec_dev *macsec)
  2088. {
  2089. struct macsec_secy *secy = &macsec->secy;
  2090. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2091. int i;
  2092. if (secy->rx_sc)
  2093. return true;
  2094. for (i = 0; i < MACSEC_NUM_AN; i++)
  2095. if (tx_sc->sa[i])
  2096. return true;
  2097. return false;
  2098. }
  2099. static int macsec_upd_offload(struct sk_buff *skb, struct genl_info *info)
  2100. {
  2101. struct nlattr *tb_offload[MACSEC_OFFLOAD_ATTR_MAX + 1];
  2102. enum macsec_offload offload, prev_offload;
  2103. int (*func)(struct macsec_context *ctx);
  2104. struct nlattr **attrs = info->attrs;
  2105. struct net_device *dev;
  2106. const struct macsec_ops *ops;
  2107. struct macsec_context ctx;
  2108. struct macsec_dev *macsec;
  2109. int ret = 0;
  2110. if (!attrs[MACSEC_ATTR_IFINDEX])
  2111. return -EINVAL;
  2112. if (!attrs[MACSEC_ATTR_OFFLOAD])
  2113. return -EINVAL;
  2114. if (nla_parse_nested_deprecated(tb_offload, MACSEC_OFFLOAD_ATTR_MAX,
  2115. attrs[MACSEC_ATTR_OFFLOAD],
  2116. macsec_genl_offload_policy, NULL))
  2117. return -EINVAL;
  2118. rtnl_lock();
  2119. dev = get_dev_from_nl(genl_info_net(info), attrs);
  2120. if (IS_ERR(dev)) {
  2121. ret = PTR_ERR(dev);
  2122. goto out;
  2123. }
  2124. macsec = macsec_priv(dev);
  2125. if (!tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]) {
  2126. ret = -EINVAL;
  2127. goto out;
  2128. }
  2129. offload = nla_get_u8(tb_offload[MACSEC_OFFLOAD_ATTR_TYPE]);
  2130. if (macsec->offload == offload)
  2131. goto out;
  2132. /* Check if the offloading mode is supported by the underlying layers */
  2133. if (offload != MACSEC_OFFLOAD_OFF &&
  2134. !macsec_check_offload(offload, macsec)) {
  2135. ret = -EOPNOTSUPP;
  2136. goto out;
  2137. }
  2138. /* Check if the net device is busy. */
  2139. if (netif_running(dev)) {
  2140. ret = -EBUSY;
  2141. goto out;
  2142. }
  2143. prev_offload = macsec->offload;
  2144. macsec->offload = offload;
  2145. /* Check if the device already has rules configured: we do not support
  2146. * rules migration.
  2147. */
  2148. if (macsec_is_configured(macsec)) {
  2149. ret = -EBUSY;
  2150. goto rollback;
  2151. }
  2152. ops = __macsec_get_ops(offload == MACSEC_OFFLOAD_OFF ? prev_offload : offload,
  2153. macsec, &ctx);
  2154. if (!ops) {
  2155. ret = -EOPNOTSUPP;
  2156. goto rollback;
  2157. }
  2158. if (prev_offload == MACSEC_OFFLOAD_OFF)
  2159. func = ops->mdo_add_secy;
  2160. else
  2161. func = ops->mdo_del_secy;
  2162. ctx.secy = &macsec->secy;
  2163. ret = macsec_offload(func, &ctx);
  2164. if (ret)
  2165. goto rollback;
  2166. rtnl_unlock();
  2167. return 0;
  2168. rollback:
  2169. macsec->offload = prev_offload;
  2170. out:
  2171. rtnl_unlock();
  2172. return ret;
  2173. }
  2174. static void get_tx_sa_stats(struct net_device *dev, int an,
  2175. struct macsec_tx_sa *tx_sa,
  2176. struct macsec_tx_sa_stats *sum)
  2177. {
  2178. struct macsec_dev *macsec = macsec_priv(dev);
  2179. int cpu;
  2180. /* If h/w offloading is available, propagate to the device */
  2181. if (macsec_is_offloaded(macsec)) {
  2182. const struct macsec_ops *ops;
  2183. struct macsec_context ctx;
  2184. ops = macsec_get_ops(macsec, &ctx);
  2185. if (ops) {
  2186. ctx.sa.assoc_num = an;
  2187. ctx.sa.tx_sa = tx_sa;
  2188. ctx.stats.tx_sa_stats = sum;
  2189. ctx.secy = &macsec_priv(dev)->secy;
  2190. macsec_offload(ops->mdo_get_tx_sa_stats, &ctx);
  2191. }
  2192. return;
  2193. }
  2194. for_each_possible_cpu(cpu) {
  2195. const struct macsec_tx_sa_stats *stats =
  2196. per_cpu_ptr(tx_sa->stats, cpu);
  2197. sum->OutPktsProtected += stats->OutPktsProtected;
  2198. sum->OutPktsEncrypted += stats->OutPktsEncrypted;
  2199. }
  2200. }
  2201. static int copy_tx_sa_stats(struct sk_buff *skb, struct macsec_tx_sa_stats *sum)
  2202. {
  2203. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED,
  2204. sum->OutPktsProtected) ||
  2205. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED,
  2206. sum->OutPktsEncrypted))
  2207. return -EMSGSIZE;
  2208. return 0;
  2209. }
  2210. static void get_rx_sa_stats(struct net_device *dev,
  2211. struct macsec_rx_sc *rx_sc, int an,
  2212. struct macsec_rx_sa *rx_sa,
  2213. struct macsec_rx_sa_stats *sum)
  2214. {
  2215. struct macsec_dev *macsec = macsec_priv(dev);
  2216. int cpu;
  2217. /* If h/w offloading is available, propagate to the device */
  2218. if (macsec_is_offloaded(macsec)) {
  2219. const struct macsec_ops *ops;
  2220. struct macsec_context ctx;
  2221. ops = macsec_get_ops(macsec, &ctx);
  2222. if (ops) {
  2223. ctx.sa.assoc_num = an;
  2224. ctx.sa.rx_sa = rx_sa;
  2225. ctx.stats.rx_sa_stats = sum;
  2226. ctx.secy = &macsec_priv(dev)->secy;
  2227. ctx.rx_sc = rx_sc;
  2228. macsec_offload(ops->mdo_get_rx_sa_stats, &ctx);
  2229. }
  2230. return;
  2231. }
  2232. for_each_possible_cpu(cpu) {
  2233. const struct macsec_rx_sa_stats *stats =
  2234. per_cpu_ptr(rx_sa->stats, cpu);
  2235. sum->InPktsOK += stats->InPktsOK;
  2236. sum->InPktsInvalid += stats->InPktsInvalid;
  2237. sum->InPktsNotValid += stats->InPktsNotValid;
  2238. sum->InPktsNotUsingSA += stats->InPktsNotUsingSA;
  2239. sum->InPktsUnusedSA += stats->InPktsUnusedSA;
  2240. }
  2241. }
  2242. static int copy_rx_sa_stats(struct sk_buff *skb,
  2243. struct macsec_rx_sa_stats *sum)
  2244. {
  2245. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum->InPktsOK) ||
  2246. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID,
  2247. sum->InPktsInvalid) ||
  2248. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID,
  2249. sum->InPktsNotValid) ||
  2250. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA,
  2251. sum->InPktsNotUsingSA) ||
  2252. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA,
  2253. sum->InPktsUnusedSA))
  2254. return -EMSGSIZE;
  2255. return 0;
  2256. }
  2257. static void get_rx_sc_stats(struct net_device *dev,
  2258. struct macsec_rx_sc *rx_sc,
  2259. struct macsec_rx_sc_stats *sum)
  2260. {
  2261. struct macsec_dev *macsec = macsec_priv(dev);
  2262. int cpu;
  2263. /* If h/w offloading is available, propagate to the device */
  2264. if (macsec_is_offloaded(macsec)) {
  2265. const struct macsec_ops *ops;
  2266. struct macsec_context ctx;
  2267. ops = macsec_get_ops(macsec, &ctx);
  2268. if (ops) {
  2269. ctx.stats.rx_sc_stats = sum;
  2270. ctx.secy = &macsec_priv(dev)->secy;
  2271. ctx.rx_sc = rx_sc;
  2272. macsec_offload(ops->mdo_get_rx_sc_stats, &ctx);
  2273. }
  2274. return;
  2275. }
  2276. for_each_possible_cpu(cpu) {
  2277. const struct pcpu_rx_sc_stats *stats;
  2278. struct macsec_rx_sc_stats tmp;
  2279. unsigned int start;
  2280. stats = per_cpu_ptr(rx_sc->stats, cpu);
  2281. do {
  2282. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2283. memcpy(&tmp, &stats->stats, sizeof(tmp));
  2284. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2285. sum->InOctetsValidated += tmp.InOctetsValidated;
  2286. sum->InOctetsDecrypted += tmp.InOctetsDecrypted;
  2287. sum->InPktsUnchecked += tmp.InPktsUnchecked;
  2288. sum->InPktsDelayed += tmp.InPktsDelayed;
  2289. sum->InPktsOK += tmp.InPktsOK;
  2290. sum->InPktsInvalid += tmp.InPktsInvalid;
  2291. sum->InPktsLate += tmp.InPktsLate;
  2292. sum->InPktsNotValid += tmp.InPktsNotValid;
  2293. sum->InPktsNotUsingSA += tmp.InPktsNotUsingSA;
  2294. sum->InPktsUnusedSA += tmp.InPktsUnusedSA;
  2295. }
  2296. }
  2297. static int copy_rx_sc_stats(struct sk_buff *skb, struct macsec_rx_sc_stats *sum)
  2298. {
  2299. if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
  2300. sum->InOctetsValidated,
  2301. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2302. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
  2303. sum->InOctetsDecrypted,
  2304. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2305. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
  2306. sum->InPktsUnchecked,
  2307. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2308. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
  2309. sum->InPktsDelayed,
  2310. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2311. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
  2312. sum->InPktsOK,
  2313. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2314. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
  2315. sum->InPktsInvalid,
  2316. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2317. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
  2318. sum->InPktsLate,
  2319. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2320. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
  2321. sum->InPktsNotValid,
  2322. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2323. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
  2324. sum->InPktsNotUsingSA,
  2325. MACSEC_RXSC_STATS_ATTR_PAD) ||
  2326. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
  2327. sum->InPktsUnusedSA,
  2328. MACSEC_RXSC_STATS_ATTR_PAD))
  2329. return -EMSGSIZE;
  2330. return 0;
  2331. }
  2332. static void get_tx_sc_stats(struct net_device *dev,
  2333. struct macsec_tx_sc_stats *sum)
  2334. {
  2335. struct macsec_dev *macsec = macsec_priv(dev);
  2336. int cpu;
  2337. /* If h/w offloading is available, propagate to the device */
  2338. if (macsec_is_offloaded(macsec)) {
  2339. const struct macsec_ops *ops;
  2340. struct macsec_context ctx;
  2341. ops = macsec_get_ops(macsec, &ctx);
  2342. if (ops) {
  2343. ctx.stats.tx_sc_stats = sum;
  2344. ctx.secy = &macsec_priv(dev)->secy;
  2345. macsec_offload(ops->mdo_get_tx_sc_stats, &ctx);
  2346. }
  2347. return;
  2348. }
  2349. for_each_possible_cpu(cpu) {
  2350. const struct pcpu_tx_sc_stats *stats;
  2351. struct macsec_tx_sc_stats tmp;
  2352. unsigned int start;
  2353. stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
  2354. do {
  2355. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2356. memcpy(&tmp, &stats->stats, sizeof(tmp));
  2357. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2358. sum->OutPktsProtected += tmp.OutPktsProtected;
  2359. sum->OutPktsEncrypted += tmp.OutPktsEncrypted;
  2360. sum->OutOctetsProtected += tmp.OutOctetsProtected;
  2361. sum->OutOctetsEncrypted += tmp.OutOctetsEncrypted;
  2362. }
  2363. }
  2364. static int copy_tx_sc_stats(struct sk_buff *skb, struct macsec_tx_sc_stats *sum)
  2365. {
  2366. if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
  2367. sum->OutPktsProtected,
  2368. MACSEC_TXSC_STATS_ATTR_PAD) ||
  2369. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
  2370. sum->OutPktsEncrypted,
  2371. MACSEC_TXSC_STATS_ATTR_PAD) ||
  2372. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
  2373. sum->OutOctetsProtected,
  2374. MACSEC_TXSC_STATS_ATTR_PAD) ||
  2375. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
  2376. sum->OutOctetsEncrypted,
  2377. MACSEC_TXSC_STATS_ATTR_PAD))
  2378. return -EMSGSIZE;
  2379. return 0;
  2380. }
  2381. static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
  2382. {
  2383. struct macsec_dev *macsec = macsec_priv(dev);
  2384. int cpu;
  2385. /* If h/w offloading is available, propagate to the device */
  2386. if (macsec_is_offloaded(macsec)) {
  2387. const struct macsec_ops *ops;
  2388. struct macsec_context ctx;
  2389. ops = macsec_get_ops(macsec, &ctx);
  2390. if (ops) {
  2391. ctx.stats.dev_stats = sum;
  2392. ctx.secy = &macsec_priv(dev)->secy;
  2393. macsec_offload(ops->mdo_get_dev_stats, &ctx);
  2394. }
  2395. return;
  2396. }
  2397. for_each_possible_cpu(cpu) {
  2398. const struct pcpu_secy_stats *stats;
  2399. struct macsec_dev_stats tmp;
  2400. unsigned int start;
  2401. stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
  2402. do {
  2403. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2404. memcpy(&tmp, &stats->stats, sizeof(tmp));
  2405. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2406. sum->OutPktsUntagged += tmp.OutPktsUntagged;
  2407. sum->InPktsUntagged += tmp.InPktsUntagged;
  2408. sum->OutPktsTooLong += tmp.OutPktsTooLong;
  2409. sum->InPktsNoTag += tmp.InPktsNoTag;
  2410. sum->InPktsBadTag += tmp.InPktsBadTag;
  2411. sum->InPktsUnknownSCI += tmp.InPktsUnknownSCI;
  2412. sum->InPktsNoSCI += tmp.InPktsNoSCI;
  2413. sum->InPktsOverrun += tmp.InPktsOverrun;
  2414. }
  2415. }
  2416. static int copy_secy_stats(struct sk_buff *skb, struct macsec_dev_stats *sum)
  2417. {
  2418. if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
  2419. sum->OutPktsUntagged,
  2420. MACSEC_SECY_STATS_ATTR_PAD) ||
  2421. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
  2422. sum->InPktsUntagged,
  2423. MACSEC_SECY_STATS_ATTR_PAD) ||
  2424. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
  2425. sum->OutPktsTooLong,
  2426. MACSEC_SECY_STATS_ATTR_PAD) ||
  2427. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
  2428. sum->InPktsNoTag,
  2429. MACSEC_SECY_STATS_ATTR_PAD) ||
  2430. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
  2431. sum->InPktsBadTag,
  2432. MACSEC_SECY_STATS_ATTR_PAD) ||
  2433. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
  2434. sum->InPktsUnknownSCI,
  2435. MACSEC_SECY_STATS_ATTR_PAD) ||
  2436. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
  2437. sum->InPktsNoSCI,
  2438. MACSEC_SECY_STATS_ATTR_PAD) ||
  2439. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
  2440. sum->InPktsOverrun,
  2441. MACSEC_SECY_STATS_ATTR_PAD))
  2442. return -EMSGSIZE;
  2443. return 0;
  2444. }
  2445. static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
  2446. {
  2447. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2448. struct nlattr *secy_nest = nla_nest_start_noflag(skb,
  2449. MACSEC_ATTR_SECY);
  2450. u64 csid;
  2451. if (!secy_nest)
  2452. return 1;
  2453. switch (secy->key_len) {
  2454. case MACSEC_GCM_AES_128_SAK_LEN:
  2455. csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
  2456. break;
  2457. case MACSEC_GCM_AES_256_SAK_LEN:
  2458. csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
  2459. break;
  2460. default:
  2461. goto cancel;
  2462. }
  2463. if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
  2464. MACSEC_SECY_ATTR_PAD) ||
  2465. nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
  2466. csid, MACSEC_SECY_ATTR_PAD) ||
  2467. nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
  2468. nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
  2469. nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
  2470. nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
  2471. nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
  2472. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
  2473. nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
  2474. nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
  2475. nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
  2476. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
  2477. goto cancel;
  2478. if (secy->replay_protect) {
  2479. if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
  2480. goto cancel;
  2481. }
  2482. nla_nest_end(skb, secy_nest);
  2483. return 0;
  2484. cancel:
  2485. nla_nest_cancel(skb, secy_nest);
  2486. return 1;
  2487. }
  2488. static noinline_for_stack int
  2489. dump_secy(struct macsec_secy *secy, struct net_device *dev,
  2490. struct sk_buff *skb, struct netlink_callback *cb)
  2491. {
  2492. struct macsec_tx_sc_stats tx_sc_stats = {0, };
  2493. struct macsec_tx_sa_stats tx_sa_stats = {0, };
  2494. struct macsec_rx_sc_stats rx_sc_stats = {0, };
  2495. struct macsec_rx_sa_stats rx_sa_stats = {0, };
  2496. struct macsec_dev *macsec = netdev_priv(dev);
  2497. struct macsec_dev_stats dev_stats = {0, };
  2498. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2499. struct nlattr *txsa_list, *rxsc_list;
  2500. struct macsec_rx_sc *rx_sc;
  2501. struct nlattr *attr;
  2502. void *hdr;
  2503. int i, j;
  2504. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2505. &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
  2506. if (!hdr)
  2507. return -EMSGSIZE;
  2508. genl_dump_check_consistent(cb, hdr);
  2509. if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
  2510. goto nla_put_failure;
  2511. attr = nla_nest_start_noflag(skb, MACSEC_ATTR_OFFLOAD);
  2512. if (!attr)
  2513. goto nla_put_failure;
  2514. if (nla_put_u8(skb, MACSEC_OFFLOAD_ATTR_TYPE, macsec->offload))
  2515. goto nla_put_failure;
  2516. nla_nest_end(skb, attr);
  2517. if (nla_put_secy(secy, skb))
  2518. goto nla_put_failure;
  2519. attr = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSC_STATS);
  2520. if (!attr)
  2521. goto nla_put_failure;
  2522. get_tx_sc_stats(dev, &tx_sc_stats);
  2523. if (copy_tx_sc_stats(skb, &tx_sc_stats)) {
  2524. nla_nest_cancel(skb, attr);
  2525. goto nla_put_failure;
  2526. }
  2527. nla_nest_end(skb, attr);
  2528. attr = nla_nest_start_noflag(skb, MACSEC_ATTR_SECY_STATS);
  2529. if (!attr)
  2530. goto nla_put_failure;
  2531. get_secy_stats(dev, &dev_stats);
  2532. if (copy_secy_stats(skb, &dev_stats)) {
  2533. nla_nest_cancel(skb, attr);
  2534. goto nla_put_failure;
  2535. }
  2536. nla_nest_end(skb, attr);
  2537. txsa_list = nla_nest_start_noflag(skb, MACSEC_ATTR_TXSA_LIST);
  2538. if (!txsa_list)
  2539. goto nla_put_failure;
  2540. for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
  2541. struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
  2542. struct nlattr *txsa_nest;
  2543. u64 pn;
  2544. int pn_len;
  2545. if (!tx_sa)
  2546. continue;
  2547. txsa_nest = nla_nest_start_noflag(skb, j++);
  2548. if (!txsa_nest) {
  2549. nla_nest_cancel(skb, txsa_list);
  2550. goto nla_put_failure;
  2551. }
  2552. attr = nla_nest_start_noflag(skb, MACSEC_SA_ATTR_STATS);
  2553. if (!attr) {
  2554. nla_nest_cancel(skb, txsa_nest);
  2555. nla_nest_cancel(skb, txsa_list);
  2556. goto nla_put_failure;
  2557. }
  2558. memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
  2559. get_tx_sa_stats(dev, i, tx_sa, &tx_sa_stats);
  2560. if (copy_tx_sa_stats(skb, &tx_sa_stats)) {
  2561. nla_nest_cancel(skb, attr);
  2562. nla_nest_cancel(skb, txsa_nest);
  2563. nla_nest_cancel(skb, txsa_list);
  2564. goto nla_put_failure;
  2565. }
  2566. nla_nest_end(skb, attr);
  2567. if (secy->xpn) {
  2568. pn = tx_sa->next_pn;
  2569. pn_len = MACSEC_XPN_PN_LEN;
  2570. } else {
  2571. pn = tx_sa->next_pn_halves.lower;
  2572. pn_len = MACSEC_DEFAULT_PN_LEN;
  2573. }
  2574. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2575. nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
  2576. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
  2577. (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, tx_sa->ssci)) ||
  2578. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
  2579. nla_nest_cancel(skb, txsa_nest);
  2580. nla_nest_cancel(skb, txsa_list);
  2581. goto nla_put_failure;
  2582. }
  2583. nla_nest_end(skb, txsa_nest);
  2584. }
  2585. nla_nest_end(skb, txsa_list);
  2586. rxsc_list = nla_nest_start_noflag(skb, MACSEC_ATTR_RXSC_LIST);
  2587. if (!rxsc_list)
  2588. goto nla_put_failure;
  2589. j = 1;
  2590. for_each_rxsc_rtnl(secy, rx_sc) {
  2591. int k;
  2592. struct nlattr *rxsa_list;
  2593. struct nlattr *rxsc_nest = nla_nest_start_noflag(skb, j++);
  2594. if (!rxsc_nest) {
  2595. nla_nest_cancel(skb, rxsc_list);
  2596. goto nla_put_failure;
  2597. }
  2598. if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
  2599. nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
  2600. MACSEC_RXSC_ATTR_PAD)) {
  2601. nla_nest_cancel(skb, rxsc_nest);
  2602. nla_nest_cancel(skb, rxsc_list);
  2603. goto nla_put_failure;
  2604. }
  2605. attr = nla_nest_start_noflag(skb, MACSEC_RXSC_ATTR_STATS);
  2606. if (!attr) {
  2607. nla_nest_cancel(skb, rxsc_nest);
  2608. nla_nest_cancel(skb, rxsc_list);
  2609. goto nla_put_failure;
  2610. }
  2611. memset(&rx_sc_stats, 0, sizeof(rx_sc_stats));
  2612. get_rx_sc_stats(dev, rx_sc, &rx_sc_stats);
  2613. if (copy_rx_sc_stats(skb, &rx_sc_stats)) {
  2614. nla_nest_cancel(skb, attr);
  2615. nla_nest_cancel(skb, rxsc_nest);
  2616. nla_nest_cancel(skb, rxsc_list);
  2617. goto nla_put_failure;
  2618. }
  2619. nla_nest_end(skb, attr);
  2620. rxsa_list = nla_nest_start_noflag(skb,
  2621. MACSEC_RXSC_ATTR_SA_LIST);
  2622. if (!rxsa_list) {
  2623. nla_nest_cancel(skb, rxsc_nest);
  2624. nla_nest_cancel(skb, rxsc_list);
  2625. goto nla_put_failure;
  2626. }
  2627. for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
  2628. struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
  2629. struct nlattr *rxsa_nest;
  2630. u64 pn;
  2631. int pn_len;
  2632. if (!rx_sa)
  2633. continue;
  2634. rxsa_nest = nla_nest_start_noflag(skb, k++);
  2635. if (!rxsa_nest) {
  2636. nla_nest_cancel(skb, rxsa_list);
  2637. nla_nest_cancel(skb, rxsc_nest);
  2638. nla_nest_cancel(skb, rxsc_list);
  2639. goto nla_put_failure;
  2640. }
  2641. attr = nla_nest_start_noflag(skb,
  2642. MACSEC_SA_ATTR_STATS);
  2643. if (!attr) {
  2644. nla_nest_cancel(skb, rxsa_list);
  2645. nla_nest_cancel(skb, rxsc_nest);
  2646. nla_nest_cancel(skb, rxsc_list);
  2647. goto nla_put_failure;
  2648. }
  2649. memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
  2650. get_rx_sa_stats(dev, rx_sc, i, rx_sa, &rx_sa_stats);
  2651. if (copy_rx_sa_stats(skb, &rx_sa_stats)) {
  2652. nla_nest_cancel(skb, attr);
  2653. nla_nest_cancel(skb, rxsa_list);
  2654. nla_nest_cancel(skb, rxsc_nest);
  2655. nla_nest_cancel(skb, rxsc_list);
  2656. goto nla_put_failure;
  2657. }
  2658. nla_nest_end(skb, attr);
  2659. if (secy->xpn) {
  2660. pn = rx_sa->next_pn;
  2661. pn_len = MACSEC_XPN_PN_LEN;
  2662. } else {
  2663. pn = rx_sa->next_pn_halves.lower;
  2664. pn_len = MACSEC_DEFAULT_PN_LEN;
  2665. }
  2666. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2667. nla_put(skb, MACSEC_SA_ATTR_PN, pn_len, &pn) ||
  2668. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
  2669. (secy->xpn && nla_put_ssci(skb, MACSEC_SA_ATTR_SSCI, rx_sa->ssci)) ||
  2670. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
  2671. nla_nest_cancel(skb, rxsa_nest);
  2672. nla_nest_cancel(skb, rxsc_nest);
  2673. nla_nest_cancel(skb, rxsc_list);
  2674. goto nla_put_failure;
  2675. }
  2676. nla_nest_end(skb, rxsa_nest);
  2677. }
  2678. nla_nest_end(skb, rxsa_list);
  2679. nla_nest_end(skb, rxsc_nest);
  2680. }
  2681. nla_nest_end(skb, rxsc_list);
  2682. genlmsg_end(skb, hdr);
  2683. return 0;
  2684. nla_put_failure:
  2685. genlmsg_cancel(skb, hdr);
  2686. return -EMSGSIZE;
  2687. }
  2688. static int macsec_generation = 1; /* protected by RTNL */
  2689. static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
  2690. {
  2691. struct net *net = sock_net(skb->sk);
  2692. struct net_device *dev;
  2693. int dev_idx, d;
  2694. dev_idx = cb->args[0];
  2695. d = 0;
  2696. rtnl_lock();
  2697. cb->seq = macsec_generation;
  2698. for_each_netdev(net, dev) {
  2699. struct macsec_secy *secy;
  2700. if (d < dev_idx)
  2701. goto next;
  2702. if (!netif_is_macsec(dev))
  2703. goto next;
  2704. secy = &macsec_priv(dev)->secy;
  2705. if (dump_secy(secy, dev, skb, cb) < 0)
  2706. goto done;
  2707. next:
  2708. d++;
  2709. }
  2710. done:
  2711. rtnl_unlock();
  2712. cb->args[0] = d;
  2713. return skb->len;
  2714. }
  2715. static const struct genl_small_ops macsec_genl_ops[] = {
  2716. {
  2717. .cmd = MACSEC_CMD_GET_TXSC,
  2718. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2719. .dumpit = macsec_dump_txsc,
  2720. },
  2721. {
  2722. .cmd = MACSEC_CMD_ADD_RXSC,
  2723. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2724. .doit = macsec_add_rxsc,
  2725. .flags = GENL_ADMIN_PERM,
  2726. },
  2727. {
  2728. .cmd = MACSEC_CMD_DEL_RXSC,
  2729. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2730. .doit = macsec_del_rxsc,
  2731. .flags = GENL_ADMIN_PERM,
  2732. },
  2733. {
  2734. .cmd = MACSEC_CMD_UPD_RXSC,
  2735. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2736. .doit = macsec_upd_rxsc,
  2737. .flags = GENL_ADMIN_PERM,
  2738. },
  2739. {
  2740. .cmd = MACSEC_CMD_ADD_TXSA,
  2741. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2742. .doit = macsec_add_txsa,
  2743. .flags = GENL_ADMIN_PERM,
  2744. },
  2745. {
  2746. .cmd = MACSEC_CMD_DEL_TXSA,
  2747. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2748. .doit = macsec_del_txsa,
  2749. .flags = GENL_ADMIN_PERM,
  2750. },
  2751. {
  2752. .cmd = MACSEC_CMD_UPD_TXSA,
  2753. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2754. .doit = macsec_upd_txsa,
  2755. .flags = GENL_ADMIN_PERM,
  2756. },
  2757. {
  2758. .cmd = MACSEC_CMD_ADD_RXSA,
  2759. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2760. .doit = macsec_add_rxsa,
  2761. .flags = GENL_ADMIN_PERM,
  2762. },
  2763. {
  2764. .cmd = MACSEC_CMD_DEL_RXSA,
  2765. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2766. .doit = macsec_del_rxsa,
  2767. .flags = GENL_ADMIN_PERM,
  2768. },
  2769. {
  2770. .cmd = MACSEC_CMD_UPD_RXSA,
  2771. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2772. .doit = macsec_upd_rxsa,
  2773. .flags = GENL_ADMIN_PERM,
  2774. },
  2775. {
  2776. .cmd = MACSEC_CMD_UPD_OFFLOAD,
  2777. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  2778. .doit = macsec_upd_offload,
  2779. .flags = GENL_ADMIN_PERM,
  2780. },
  2781. };
  2782. static struct genl_family macsec_fam __ro_after_init = {
  2783. .name = MACSEC_GENL_NAME,
  2784. .hdrsize = 0,
  2785. .version = MACSEC_GENL_VERSION,
  2786. .maxattr = MACSEC_ATTR_MAX,
  2787. .policy = macsec_genl_policy,
  2788. .netnsok = true,
  2789. .module = THIS_MODULE,
  2790. .small_ops = macsec_genl_ops,
  2791. .n_small_ops = ARRAY_SIZE(macsec_genl_ops),
  2792. .resv_start_op = MACSEC_CMD_UPD_OFFLOAD + 1,
  2793. };
  2794. static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
  2795. struct net_device *dev)
  2796. {
  2797. struct macsec_dev *macsec = netdev_priv(dev);
  2798. struct macsec_secy *secy = &macsec->secy;
  2799. struct pcpu_secy_stats *secy_stats;
  2800. int ret, len;
  2801. if (macsec_is_offloaded(netdev_priv(dev))) {
  2802. struct metadata_dst *md_dst = secy->tx_sc.md_dst;
  2803. skb_dst_drop(skb);
  2804. dst_hold(&md_dst->dst);
  2805. skb_dst_set(skb, &md_dst->dst);
  2806. skb->dev = macsec->real_dev;
  2807. return dev_queue_xmit(skb);
  2808. }
  2809. /* 10.5 */
  2810. if (!secy->protect_frames) {
  2811. secy_stats = this_cpu_ptr(macsec->stats);
  2812. u64_stats_update_begin(&secy_stats->syncp);
  2813. secy_stats->stats.OutPktsUntagged++;
  2814. u64_stats_update_end(&secy_stats->syncp);
  2815. skb->dev = macsec->real_dev;
  2816. len = skb->len;
  2817. ret = dev_queue_xmit(skb);
  2818. count_tx(dev, ret, len);
  2819. return ret;
  2820. }
  2821. if (!secy->operational) {
  2822. kfree_skb(skb);
  2823. DEV_STATS_INC(dev, tx_dropped);
  2824. return NETDEV_TX_OK;
  2825. }
  2826. len = skb->len;
  2827. skb = macsec_encrypt(skb, dev);
  2828. if (IS_ERR(skb)) {
  2829. if (PTR_ERR(skb) != -EINPROGRESS)
  2830. DEV_STATS_INC(dev, tx_dropped);
  2831. return NETDEV_TX_OK;
  2832. }
  2833. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  2834. macsec_encrypt_finish(skb, dev);
  2835. ret = dev_queue_xmit(skb);
  2836. count_tx(dev, ret, len);
  2837. return ret;
  2838. }
  2839. #define MACSEC_FEATURES \
  2840. (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
  2841. static int macsec_dev_init(struct net_device *dev)
  2842. {
  2843. struct macsec_dev *macsec = macsec_priv(dev);
  2844. struct net_device *real_dev = macsec->real_dev;
  2845. int err;
  2846. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  2847. if (!dev->tstats)
  2848. return -ENOMEM;
  2849. err = gro_cells_init(&macsec->gro_cells, dev);
  2850. if (err) {
  2851. free_percpu(dev->tstats);
  2852. return err;
  2853. }
  2854. dev->features = real_dev->features & MACSEC_FEATURES;
  2855. dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
  2856. dev->needed_headroom = real_dev->needed_headroom +
  2857. MACSEC_NEEDED_HEADROOM;
  2858. dev->needed_tailroom = real_dev->needed_tailroom +
  2859. MACSEC_NEEDED_TAILROOM;
  2860. if (is_zero_ether_addr(dev->dev_addr))
  2861. eth_hw_addr_inherit(dev, real_dev);
  2862. if (is_zero_ether_addr(dev->broadcast))
  2863. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  2864. /* Get macsec's reference to real_dev */
  2865. netdev_hold(real_dev, &macsec->dev_tracker, GFP_KERNEL);
  2866. return 0;
  2867. }
  2868. static void macsec_dev_uninit(struct net_device *dev)
  2869. {
  2870. struct macsec_dev *macsec = macsec_priv(dev);
  2871. gro_cells_destroy(&macsec->gro_cells);
  2872. free_percpu(dev->tstats);
  2873. }
  2874. static netdev_features_t macsec_fix_features(struct net_device *dev,
  2875. netdev_features_t features)
  2876. {
  2877. struct macsec_dev *macsec = macsec_priv(dev);
  2878. struct net_device *real_dev = macsec->real_dev;
  2879. features &= (real_dev->features & MACSEC_FEATURES) |
  2880. NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
  2881. features |= NETIF_F_LLTX;
  2882. return features;
  2883. }
  2884. static int macsec_dev_open(struct net_device *dev)
  2885. {
  2886. struct macsec_dev *macsec = macsec_priv(dev);
  2887. struct net_device *real_dev = macsec->real_dev;
  2888. int err;
  2889. err = dev_uc_add(real_dev, dev->dev_addr);
  2890. if (err < 0)
  2891. return err;
  2892. if (dev->flags & IFF_ALLMULTI) {
  2893. err = dev_set_allmulti(real_dev, 1);
  2894. if (err < 0)
  2895. goto del_unicast;
  2896. }
  2897. if (dev->flags & IFF_PROMISC) {
  2898. err = dev_set_promiscuity(real_dev, 1);
  2899. if (err < 0)
  2900. goto clear_allmulti;
  2901. }
  2902. /* If h/w offloading is available, propagate to the device */
  2903. if (macsec_is_offloaded(macsec)) {
  2904. const struct macsec_ops *ops;
  2905. struct macsec_context ctx;
  2906. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  2907. if (!ops) {
  2908. err = -EOPNOTSUPP;
  2909. goto clear_allmulti;
  2910. }
  2911. ctx.secy = &macsec->secy;
  2912. err = macsec_offload(ops->mdo_dev_open, &ctx);
  2913. if (err)
  2914. goto clear_allmulti;
  2915. }
  2916. if (netif_carrier_ok(real_dev))
  2917. netif_carrier_on(dev);
  2918. return 0;
  2919. clear_allmulti:
  2920. if (dev->flags & IFF_ALLMULTI)
  2921. dev_set_allmulti(real_dev, -1);
  2922. del_unicast:
  2923. dev_uc_del(real_dev, dev->dev_addr);
  2924. netif_carrier_off(dev);
  2925. return err;
  2926. }
  2927. static int macsec_dev_stop(struct net_device *dev)
  2928. {
  2929. struct macsec_dev *macsec = macsec_priv(dev);
  2930. struct net_device *real_dev = macsec->real_dev;
  2931. netif_carrier_off(dev);
  2932. /* If h/w offloading is available, propagate to the device */
  2933. if (macsec_is_offloaded(macsec)) {
  2934. const struct macsec_ops *ops;
  2935. struct macsec_context ctx;
  2936. ops = macsec_get_ops(macsec, &ctx);
  2937. if (ops) {
  2938. ctx.secy = &macsec->secy;
  2939. macsec_offload(ops->mdo_dev_stop, &ctx);
  2940. }
  2941. }
  2942. dev_mc_unsync(real_dev, dev);
  2943. dev_uc_unsync(real_dev, dev);
  2944. if (dev->flags & IFF_ALLMULTI)
  2945. dev_set_allmulti(real_dev, -1);
  2946. if (dev->flags & IFF_PROMISC)
  2947. dev_set_promiscuity(real_dev, -1);
  2948. dev_uc_del(real_dev, dev->dev_addr);
  2949. return 0;
  2950. }
  2951. static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
  2952. {
  2953. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2954. if (!(dev->flags & IFF_UP))
  2955. return;
  2956. if (change & IFF_ALLMULTI)
  2957. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  2958. if (change & IFF_PROMISC)
  2959. dev_set_promiscuity(real_dev,
  2960. dev->flags & IFF_PROMISC ? 1 : -1);
  2961. }
  2962. static void macsec_dev_set_rx_mode(struct net_device *dev)
  2963. {
  2964. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2965. dev_mc_sync(real_dev, dev);
  2966. dev_uc_sync(real_dev, dev);
  2967. }
  2968. static int macsec_set_mac_address(struct net_device *dev, void *p)
  2969. {
  2970. struct macsec_dev *macsec = macsec_priv(dev);
  2971. struct net_device *real_dev = macsec->real_dev;
  2972. struct sockaddr *addr = p;
  2973. int err;
  2974. if (!is_valid_ether_addr(addr->sa_data))
  2975. return -EADDRNOTAVAIL;
  2976. if (!(dev->flags & IFF_UP))
  2977. goto out;
  2978. err = dev_uc_add(real_dev, addr->sa_data);
  2979. if (err < 0)
  2980. return err;
  2981. dev_uc_del(real_dev, dev->dev_addr);
  2982. out:
  2983. eth_hw_addr_set(dev, addr->sa_data);
  2984. /* If h/w offloading is available, propagate to the device */
  2985. if (macsec_is_offloaded(macsec)) {
  2986. const struct macsec_ops *ops;
  2987. struct macsec_context ctx;
  2988. ops = macsec_get_ops(macsec, &ctx);
  2989. if (ops) {
  2990. ctx.secy = &macsec->secy;
  2991. macsec_offload(ops->mdo_upd_secy, &ctx);
  2992. }
  2993. }
  2994. return 0;
  2995. }
  2996. static int macsec_change_mtu(struct net_device *dev, int new_mtu)
  2997. {
  2998. struct macsec_dev *macsec = macsec_priv(dev);
  2999. unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
  3000. if (macsec->real_dev->mtu - extra < new_mtu)
  3001. return -ERANGE;
  3002. dev->mtu = new_mtu;
  3003. return 0;
  3004. }
  3005. static void macsec_get_stats64(struct net_device *dev,
  3006. struct rtnl_link_stats64 *s)
  3007. {
  3008. if (!dev->tstats)
  3009. return;
  3010. dev_fetch_sw_netstats(s, dev->tstats);
  3011. s->rx_dropped = DEV_STATS_READ(dev, rx_dropped);
  3012. s->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
  3013. s->rx_errors = DEV_STATS_READ(dev, rx_errors);
  3014. }
  3015. static int macsec_get_iflink(const struct net_device *dev)
  3016. {
  3017. return macsec_priv(dev)->real_dev->ifindex;
  3018. }
  3019. static const struct net_device_ops macsec_netdev_ops = {
  3020. .ndo_init = macsec_dev_init,
  3021. .ndo_uninit = macsec_dev_uninit,
  3022. .ndo_open = macsec_dev_open,
  3023. .ndo_stop = macsec_dev_stop,
  3024. .ndo_fix_features = macsec_fix_features,
  3025. .ndo_change_mtu = macsec_change_mtu,
  3026. .ndo_set_rx_mode = macsec_dev_set_rx_mode,
  3027. .ndo_change_rx_flags = macsec_dev_change_rx_flags,
  3028. .ndo_set_mac_address = macsec_set_mac_address,
  3029. .ndo_start_xmit = macsec_start_xmit,
  3030. .ndo_get_stats64 = macsec_get_stats64,
  3031. .ndo_get_iflink = macsec_get_iflink,
  3032. };
  3033. static const struct device_type macsec_type = {
  3034. .name = "macsec",
  3035. };
  3036. static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
  3037. [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
  3038. [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
  3039. [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
  3040. [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
  3041. [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
  3042. [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
  3043. [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
  3044. [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
  3045. [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
  3046. [IFLA_MACSEC_ES] = { .type = NLA_U8 },
  3047. [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
  3048. [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
  3049. [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
  3050. [IFLA_MACSEC_OFFLOAD] = { .type = NLA_U8 },
  3051. };
  3052. static void macsec_free_netdev(struct net_device *dev)
  3053. {
  3054. struct macsec_dev *macsec = macsec_priv(dev);
  3055. if (macsec->secy.tx_sc.md_dst)
  3056. metadata_dst_free(macsec->secy.tx_sc.md_dst);
  3057. free_percpu(macsec->stats);
  3058. free_percpu(macsec->secy.tx_sc.stats);
  3059. /* Get rid of the macsec's reference to real_dev */
  3060. netdev_put(macsec->real_dev, &macsec->dev_tracker);
  3061. }
  3062. static void macsec_setup(struct net_device *dev)
  3063. {
  3064. ether_setup(dev);
  3065. dev->min_mtu = 0;
  3066. dev->max_mtu = ETH_MAX_MTU;
  3067. dev->priv_flags |= IFF_NO_QUEUE;
  3068. dev->netdev_ops = &macsec_netdev_ops;
  3069. dev->needs_free_netdev = true;
  3070. dev->priv_destructor = macsec_free_netdev;
  3071. SET_NETDEV_DEVTYPE(dev, &macsec_type);
  3072. eth_zero_addr(dev->broadcast);
  3073. }
  3074. static int macsec_changelink_common(struct net_device *dev,
  3075. struct nlattr *data[])
  3076. {
  3077. struct macsec_secy *secy;
  3078. struct macsec_tx_sc *tx_sc;
  3079. secy = &macsec_priv(dev)->secy;
  3080. tx_sc = &secy->tx_sc;
  3081. if (data[IFLA_MACSEC_ENCODING_SA]) {
  3082. struct macsec_tx_sa *tx_sa;
  3083. tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
  3084. tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
  3085. secy->operational = tx_sa && tx_sa->active;
  3086. }
  3087. if (data[IFLA_MACSEC_ENCRYPT])
  3088. tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
  3089. if (data[IFLA_MACSEC_PROTECT])
  3090. secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
  3091. if (data[IFLA_MACSEC_INC_SCI])
  3092. tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
  3093. if (data[IFLA_MACSEC_ES])
  3094. tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
  3095. if (data[IFLA_MACSEC_SCB])
  3096. tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
  3097. if (data[IFLA_MACSEC_REPLAY_PROTECT])
  3098. secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
  3099. if (data[IFLA_MACSEC_VALIDATION])
  3100. secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
  3101. if (data[IFLA_MACSEC_CIPHER_SUITE]) {
  3102. switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
  3103. case MACSEC_CIPHER_ID_GCM_AES_128:
  3104. case MACSEC_DEFAULT_CIPHER_ID:
  3105. secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
  3106. secy->xpn = false;
  3107. break;
  3108. case MACSEC_CIPHER_ID_GCM_AES_256:
  3109. secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
  3110. secy->xpn = false;
  3111. break;
  3112. case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
  3113. secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
  3114. secy->xpn = true;
  3115. break;
  3116. case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
  3117. secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
  3118. secy->xpn = true;
  3119. break;
  3120. default:
  3121. return -EINVAL;
  3122. }
  3123. }
  3124. if (data[IFLA_MACSEC_WINDOW]) {
  3125. secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
  3126. /* IEEE 802.1AEbw-2013 10.7.8 - maximum replay window
  3127. * for XPN cipher suites */
  3128. if (secy->xpn &&
  3129. secy->replay_window > MACSEC_XPN_MAX_REPLAY_WINDOW)
  3130. return -EINVAL;
  3131. }
  3132. return 0;
  3133. }
  3134. static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
  3135. struct nlattr *data[],
  3136. struct netlink_ext_ack *extack)
  3137. {
  3138. struct macsec_dev *macsec = macsec_priv(dev);
  3139. struct macsec_tx_sc tx_sc;
  3140. struct macsec_secy secy;
  3141. int ret;
  3142. if (!data)
  3143. return 0;
  3144. if (data[IFLA_MACSEC_CIPHER_SUITE] ||
  3145. data[IFLA_MACSEC_ICV_LEN] ||
  3146. data[IFLA_MACSEC_SCI] ||
  3147. data[IFLA_MACSEC_PORT])
  3148. return -EINVAL;
  3149. /* Keep a copy of unmodified secy and tx_sc, in case the offload
  3150. * propagation fails, to revert macsec_changelink_common.
  3151. */
  3152. memcpy(&secy, &macsec->secy, sizeof(secy));
  3153. memcpy(&tx_sc, &macsec->secy.tx_sc, sizeof(tx_sc));
  3154. ret = macsec_changelink_common(dev, data);
  3155. if (ret)
  3156. goto cleanup;
  3157. /* If h/w offloading is available, propagate to the device */
  3158. if (macsec_is_offloaded(macsec)) {
  3159. const struct macsec_ops *ops;
  3160. struct macsec_context ctx;
  3161. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  3162. if (!ops) {
  3163. ret = -EOPNOTSUPP;
  3164. goto cleanup;
  3165. }
  3166. ctx.secy = &macsec->secy;
  3167. ret = macsec_offload(ops->mdo_upd_secy, &ctx);
  3168. if (ret)
  3169. goto cleanup;
  3170. }
  3171. return 0;
  3172. cleanup:
  3173. memcpy(&macsec->secy.tx_sc, &tx_sc, sizeof(tx_sc));
  3174. memcpy(&macsec->secy, &secy, sizeof(secy));
  3175. return ret;
  3176. }
  3177. static void macsec_del_dev(struct macsec_dev *macsec)
  3178. {
  3179. int i;
  3180. while (macsec->secy.rx_sc) {
  3181. struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
  3182. rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
  3183. free_rx_sc(rx_sc);
  3184. }
  3185. for (i = 0; i < MACSEC_NUM_AN; i++) {
  3186. struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
  3187. if (sa) {
  3188. RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
  3189. clear_tx_sa(sa);
  3190. }
  3191. }
  3192. }
  3193. static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
  3194. {
  3195. struct macsec_dev *macsec = macsec_priv(dev);
  3196. struct net_device *real_dev = macsec->real_dev;
  3197. /* If h/w offloading is available, propagate to the device */
  3198. if (macsec_is_offloaded(macsec)) {
  3199. const struct macsec_ops *ops;
  3200. struct macsec_context ctx;
  3201. ops = macsec_get_ops(netdev_priv(dev), &ctx);
  3202. if (ops) {
  3203. ctx.secy = &macsec->secy;
  3204. macsec_offload(ops->mdo_del_secy, &ctx);
  3205. }
  3206. }
  3207. unregister_netdevice_queue(dev, head);
  3208. list_del_rcu(&macsec->secys);
  3209. macsec_del_dev(macsec);
  3210. netdev_upper_dev_unlink(real_dev, dev);
  3211. macsec_generation++;
  3212. }
  3213. static void macsec_dellink(struct net_device *dev, struct list_head *head)
  3214. {
  3215. struct macsec_dev *macsec = macsec_priv(dev);
  3216. struct net_device *real_dev = macsec->real_dev;
  3217. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  3218. macsec_common_dellink(dev, head);
  3219. if (list_empty(&rxd->secys)) {
  3220. netdev_rx_handler_unregister(real_dev);
  3221. kfree(rxd);
  3222. }
  3223. }
  3224. static int register_macsec_dev(struct net_device *real_dev,
  3225. struct net_device *dev)
  3226. {
  3227. struct macsec_dev *macsec = macsec_priv(dev);
  3228. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  3229. if (!rxd) {
  3230. int err;
  3231. rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
  3232. if (!rxd)
  3233. return -ENOMEM;
  3234. INIT_LIST_HEAD(&rxd->secys);
  3235. err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
  3236. rxd);
  3237. if (err < 0) {
  3238. kfree(rxd);
  3239. return err;
  3240. }
  3241. }
  3242. list_add_tail_rcu(&macsec->secys, &rxd->secys);
  3243. return 0;
  3244. }
  3245. static bool sci_exists(struct net_device *dev, sci_t sci)
  3246. {
  3247. struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
  3248. struct macsec_dev *macsec;
  3249. list_for_each_entry(macsec, &rxd->secys, secys) {
  3250. if (macsec->secy.sci == sci)
  3251. return true;
  3252. }
  3253. return false;
  3254. }
  3255. static sci_t dev_to_sci(struct net_device *dev, __be16 port)
  3256. {
  3257. return make_sci(dev->dev_addr, port);
  3258. }
  3259. static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
  3260. {
  3261. struct macsec_dev *macsec = macsec_priv(dev);
  3262. struct macsec_secy *secy = &macsec->secy;
  3263. macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
  3264. if (!macsec->stats)
  3265. return -ENOMEM;
  3266. secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
  3267. if (!secy->tx_sc.stats)
  3268. return -ENOMEM;
  3269. secy->tx_sc.md_dst = metadata_dst_alloc(0, METADATA_MACSEC, GFP_KERNEL);
  3270. if (!secy->tx_sc.md_dst)
  3271. /* macsec and secy percpu stats will be freed when unregistering
  3272. * net_device in macsec_free_netdev()
  3273. */
  3274. return -ENOMEM;
  3275. if (sci == MACSEC_UNDEF_SCI)
  3276. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  3277. secy->netdev = dev;
  3278. secy->operational = true;
  3279. secy->key_len = DEFAULT_SAK_LEN;
  3280. secy->icv_len = icv_len;
  3281. secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
  3282. secy->protect_frames = true;
  3283. secy->replay_protect = false;
  3284. secy->xpn = DEFAULT_XPN;
  3285. secy->sci = sci;
  3286. secy->tx_sc.md_dst->u.macsec_info.sci = sci;
  3287. secy->tx_sc.active = true;
  3288. secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
  3289. secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
  3290. secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
  3291. secy->tx_sc.end_station = false;
  3292. secy->tx_sc.scb = false;
  3293. return 0;
  3294. }
  3295. static struct lock_class_key macsec_netdev_addr_lock_key;
  3296. static int macsec_newlink(struct net *net, struct net_device *dev,
  3297. struct nlattr *tb[], struct nlattr *data[],
  3298. struct netlink_ext_ack *extack)
  3299. {
  3300. struct macsec_dev *macsec = macsec_priv(dev);
  3301. rx_handler_func_t *rx_handler;
  3302. u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
  3303. struct net_device *real_dev;
  3304. int err, mtu;
  3305. sci_t sci;
  3306. if (!tb[IFLA_LINK])
  3307. return -EINVAL;
  3308. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  3309. if (!real_dev)
  3310. return -ENODEV;
  3311. if (real_dev->type != ARPHRD_ETHER)
  3312. return -EINVAL;
  3313. dev->priv_flags |= IFF_MACSEC;
  3314. macsec->real_dev = real_dev;
  3315. if (data && data[IFLA_MACSEC_OFFLOAD])
  3316. macsec->offload = nla_get_offload(data[IFLA_MACSEC_OFFLOAD]);
  3317. else
  3318. /* MACsec offloading is off by default */
  3319. macsec->offload = MACSEC_OFFLOAD_OFF;
  3320. /* Check if the offloading mode is supported by the underlying layers */
  3321. if (macsec->offload != MACSEC_OFFLOAD_OFF &&
  3322. !macsec_check_offload(macsec->offload, macsec))
  3323. return -EOPNOTSUPP;
  3324. /* send_sci must be set to true when transmit sci explicitly is set */
  3325. if ((data && data[IFLA_MACSEC_SCI]) &&
  3326. (data && data[IFLA_MACSEC_INC_SCI])) {
  3327. u8 send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
  3328. if (!send_sci)
  3329. return -EINVAL;
  3330. }
  3331. if (data && data[IFLA_MACSEC_ICV_LEN])
  3332. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  3333. mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
  3334. if (mtu < 0)
  3335. dev->mtu = 0;
  3336. else
  3337. dev->mtu = mtu;
  3338. rx_handler = rtnl_dereference(real_dev->rx_handler);
  3339. if (rx_handler && rx_handler != macsec_handle_frame)
  3340. return -EBUSY;
  3341. err = register_netdevice(dev);
  3342. if (err < 0)
  3343. return err;
  3344. netdev_lockdep_set_classes(dev);
  3345. lockdep_set_class(&dev->addr_list_lock,
  3346. &macsec_netdev_addr_lock_key);
  3347. err = netdev_upper_dev_link(real_dev, dev, extack);
  3348. if (err < 0)
  3349. goto unregister;
  3350. /* need to be already registered so that ->init has run and
  3351. * the MAC addr is set
  3352. */
  3353. if (data && data[IFLA_MACSEC_SCI])
  3354. sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
  3355. else if (data && data[IFLA_MACSEC_PORT])
  3356. sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
  3357. else
  3358. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  3359. if (rx_handler && sci_exists(real_dev, sci)) {
  3360. err = -EBUSY;
  3361. goto unlink;
  3362. }
  3363. err = macsec_add_dev(dev, sci, icv_len);
  3364. if (err)
  3365. goto unlink;
  3366. if (data) {
  3367. err = macsec_changelink_common(dev, data);
  3368. if (err)
  3369. goto del_dev;
  3370. }
  3371. /* If h/w offloading is available, propagate to the device */
  3372. if (macsec_is_offloaded(macsec)) {
  3373. const struct macsec_ops *ops;
  3374. struct macsec_context ctx;
  3375. ops = macsec_get_ops(macsec, &ctx);
  3376. if (ops) {
  3377. ctx.secy = &macsec->secy;
  3378. err = macsec_offload(ops->mdo_add_secy, &ctx);
  3379. if (err)
  3380. goto del_dev;
  3381. }
  3382. }
  3383. err = register_macsec_dev(real_dev, dev);
  3384. if (err < 0)
  3385. goto del_dev;
  3386. netif_stacked_transfer_operstate(real_dev, dev);
  3387. linkwatch_fire_event(dev);
  3388. macsec_generation++;
  3389. return 0;
  3390. del_dev:
  3391. macsec_del_dev(macsec);
  3392. unlink:
  3393. netdev_upper_dev_unlink(real_dev, dev);
  3394. unregister:
  3395. unregister_netdevice(dev);
  3396. return err;
  3397. }
  3398. static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
  3399. struct netlink_ext_ack *extack)
  3400. {
  3401. u64 csid = MACSEC_DEFAULT_CIPHER_ID;
  3402. u8 icv_len = MACSEC_DEFAULT_ICV_LEN;
  3403. int flag;
  3404. bool es, scb, sci;
  3405. if (!data)
  3406. return 0;
  3407. if (data[IFLA_MACSEC_CIPHER_SUITE])
  3408. csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
  3409. if (data[IFLA_MACSEC_ICV_LEN]) {
  3410. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  3411. if (icv_len != MACSEC_DEFAULT_ICV_LEN) {
  3412. char dummy_key[DEFAULT_SAK_LEN] = { 0 };
  3413. struct crypto_aead *dummy_tfm;
  3414. dummy_tfm = macsec_alloc_tfm(dummy_key,
  3415. DEFAULT_SAK_LEN,
  3416. icv_len);
  3417. if (IS_ERR(dummy_tfm))
  3418. return PTR_ERR(dummy_tfm);
  3419. crypto_free_aead(dummy_tfm);
  3420. }
  3421. }
  3422. switch (csid) {
  3423. case MACSEC_CIPHER_ID_GCM_AES_128:
  3424. case MACSEC_CIPHER_ID_GCM_AES_256:
  3425. case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
  3426. case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
  3427. case MACSEC_DEFAULT_CIPHER_ID:
  3428. if (icv_len < MACSEC_MIN_ICV_LEN ||
  3429. icv_len > MACSEC_STD_ICV_LEN)
  3430. return -EINVAL;
  3431. break;
  3432. default:
  3433. return -EINVAL;
  3434. }
  3435. if (data[IFLA_MACSEC_ENCODING_SA]) {
  3436. if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
  3437. return -EINVAL;
  3438. }
  3439. for (flag = IFLA_MACSEC_ENCODING_SA + 1;
  3440. flag < IFLA_MACSEC_VALIDATION;
  3441. flag++) {
  3442. if (data[flag]) {
  3443. if (nla_get_u8(data[flag]) > 1)
  3444. return -EINVAL;
  3445. }
  3446. }
  3447. es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
  3448. sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
  3449. scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
  3450. if ((sci && (scb || es)) || (scb && es))
  3451. return -EINVAL;
  3452. if (data[IFLA_MACSEC_VALIDATION] &&
  3453. nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
  3454. return -EINVAL;
  3455. if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
  3456. nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
  3457. !data[IFLA_MACSEC_WINDOW])
  3458. return -EINVAL;
  3459. return 0;
  3460. }
  3461. static struct net *macsec_get_link_net(const struct net_device *dev)
  3462. {
  3463. return dev_net(macsec_priv(dev)->real_dev);
  3464. }
  3465. static size_t macsec_get_size(const struct net_device *dev)
  3466. {
  3467. return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
  3468. nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
  3469. nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
  3470. nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
  3471. nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
  3472. nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
  3473. nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
  3474. nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
  3475. nla_total_size(1) + /* IFLA_MACSEC_ES */
  3476. nla_total_size(1) + /* IFLA_MACSEC_SCB */
  3477. nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
  3478. nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
  3479. 0;
  3480. }
  3481. static int macsec_fill_info(struct sk_buff *skb,
  3482. const struct net_device *dev)
  3483. {
  3484. struct macsec_secy *secy = &macsec_priv(dev)->secy;
  3485. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  3486. u64 csid;
  3487. switch (secy->key_len) {
  3488. case MACSEC_GCM_AES_128_SAK_LEN:
  3489. csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_128 : MACSEC_DEFAULT_CIPHER_ID;
  3490. break;
  3491. case MACSEC_GCM_AES_256_SAK_LEN:
  3492. csid = secy->xpn ? MACSEC_CIPHER_ID_GCM_AES_XPN_256 : MACSEC_CIPHER_ID_GCM_AES_256;
  3493. break;
  3494. default:
  3495. goto nla_put_failure;
  3496. }
  3497. if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
  3498. IFLA_MACSEC_PAD) ||
  3499. nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
  3500. nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
  3501. csid, IFLA_MACSEC_PAD) ||
  3502. nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
  3503. nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
  3504. nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
  3505. nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
  3506. nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
  3507. nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
  3508. nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
  3509. nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
  3510. 0)
  3511. goto nla_put_failure;
  3512. if (secy->replay_protect) {
  3513. if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
  3514. goto nla_put_failure;
  3515. }
  3516. return 0;
  3517. nla_put_failure:
  3518. return -EMSGSIZE;
  3519. }
  3520. static struct rtnl_link_ops macsec_link_ops __read_mostly = {
  3521. .kind = "macsec",
  3522. .priv_size = sizeof(struct macsec_dev),
  3523. .maxtype = IFLA_MACSEC_MAX,
  3524. .policy = macsec_rtnl_policy,
  3525. .setup = macsec_setup,
  3526. .validate = macsec_validate_attr,
  3527. .newlink = macsec_newlink,
  3528. .changelink = macsec_changelink,
  3529. .dellink = macsec_dellink,
  3530. .get_size = macsec_get_size,
  3531. .fill_info = macsec_fill_info,
  3532. .get_link_net = macsec_get_link_net,
  3533. };
  3534. static bool is_macsec_master(struct net_device *dev)
  3535. {
  3536. return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
  3537. }
  3538. static int macsec_notify(struct notifier_block *this, unsigned long event,
  3539. void *ptr)
  3540. {
  3541. struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
  3542. LIST_HEAD(head);
  3543. if (!is_macsec_master(real_dev))
  3544. return NOTIFY_DONE;
  3545. switch (event) {
  3546. case NETDEV_DOWN:
  3547. case NETDEV_UP:
  3548. case NETDEV_CHANGE: {
  3549. struct macsec_dev *m, *n;
  3550. struct macsec_rxh_data *rxd;
  3551. rxd = macsec_data_rtnl(real_dev);
  3552. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  3553. struct net_device *dev = m->secy.netdev;
  3554. netif_stacked_transfer_operstate(real_dev, dev);
  3555. }
  3556. break;
  3557. }
  3558. case NETDEV_UNREGISTER: {
  3559. struct macsec_dev *m, *n;
  3560. struct macsec_rxh_data *rxd;
  3561. rxd = macsec_data_rtnl(real_dev);
  3562. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  3563. macsec_common_dellink(m->secy.netdev, &head);
  3564. }
  3565. netdev_rx_handler_unregister(real_dev);
  3566. kfree(rxd);
  3567. unregister_netdevice_many(&head);
  3568. break;
  3569. }
  3570. case NETDEV_CHANGEMTU: {
  3571. struct macsec_dev *m;
  3572. struct macsec_rxh_data *rxd;
  3573. rxd = macsec_data_rtnl(real_dev);
  3574. list_for_each_entry(m, &rxd->secys, secys) {
  3575. struct net_device *dev = m->secy.netdev;
  3576. unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
  3577. macsec_extra_len(true));
  3578. if (dev->mtu > mtu)
  3579. dev_set_mtu(dev, mtu);
  3580. }
  3581. }
  3582. }
  3583. return NOTIFY_OK;
  3584. }
  3585. static struct notifier_block macsec_notifier = {
  3586. .notifier_call = macsec_notify,
  3587. };
  3588. static int __init macsec_init(void)
  3589. {
  3590. int err;
  3591. pr_info("MACsec IEEE 802.1AE\n");
  3592. err = register_netdevice_notifier(&macsec_notifier);
  3593. if (err)
  3594. return err;
  3595. err = rtnl_link_register(&macsec_link_ops);
  3596. if (err)
  3597. goto notifier;
  3598. err = genl_register_family(&macsec_fam);
  3599. if (err)
  3600. goto rtnl;
  3601. return 0;
  3602. rtnl:
  3603. rtnl_link_unregister(&macsec_link_ops);
  3604. notifier:
  3605. unregister_netdevice_notifier(&macsec_notifier);
  3606. return err;
  3607. }
  3608. static void __exit macsec_exit(void)
  3609. {
  3610. genl_unregister_family(&macsec_fam);
  3611. rtnl_link_unregister(&macsec_link_ops);
  3612. unregister_netdevice_notifier(&macsec_notifier);
  3613. rcu_barrier();
  3614. }
  3615. module_init(macsec_init);
  3616. module_exit(macsec_exit);
  3617. MODULE_ALIAS_RTNL_LINK("macsec");
  3618. MODULE_ALIAS_GENL_FAMILY("macsec");
  3619. MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
  3620. MODULE_LICENSE("GPL v2");