macsec.c 111 KB

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