macsec.c 110 KB

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