macsec.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575
  1. /*
  2. * drivers/net/macsec.c - MACsec device
  3. *
  4. * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/socket.h>
  14. #include <linux/module.h>
  15. #include <crypto/aead.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/refcount.h>
  19. #include <net/genetlink.h>
  20. #include <net/sock.h>
  21. #include <net/gro_cells.h>
  22. #include <uapi/linux/if_macsec.h>
  23. typedef u64 __bitwise sci_t;
  24. #define MACSEC_SCI_LEN 8
  25. /* SecTAG length = macsec_eth_header without the optional SCI */
  26. #define MACSEC_TAG_LEN 6
  27. struct macsec_eth_header {
  28. struct ethhdr eth;
  29. /* SecTAG */
  30. u8 tci_an;
  31. #if defined(__LITTLE_ENDIAN_BITFIELD)
  32. u8 short_length:6,
  33. unused:2;
  34. #elif defined(__BIG_ENDIAN_BITFIELD)
  35. u8 unused:2,
  36. short_length:6;
  37. #else
  38. #error "Please fix <asm/byteorder.h>"
  39. #endif
  40. __be32 packet_number;
  41. u8 secure_channel_id[8]; /* optional */
  42. } __packed;
  43. #define MACSEC_TCI_VERSION 0x80
  44. #define MACSEC_TCI_ES 0x40 /* end station */
  45. #define MACSEC_TCI_SC 0x20 /* SCI present */
  46. #define MACSEC_TCI_SCB 0x10 /* epon */
  47. #define MACSEC_TCI_E 0x08 /* encryption */
  48. #define MACSEC_TCI_C 0x04 /* changed text */
  49. #define MACSEC_AN_MASK 0x03 /* association number */
  50. #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
  51. /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
  52. #define MIN_NON_SHORT_LEN 48
  53. #define GCM_AES_IV_LEN 12
  54. #define DEFAULT_ICV_LEN 16
  55. #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
  56. #define for_each_rxsc(secy, sc) \
  57. for (sc = rcu_dereference_bh(secy->rx_sc); \
  58. sc; \
  59. sc = rcu_dereference_bh(sc->next))
  60. #define for_each_rxsc_rtnl(secy, sc) \
  61. for (sc = rtnl_dereference(secy->rx_sc); \
  62. sc; \
  63. sc = rtnl_dereference(sc->next))
  64. struct gcm_iv {
  65. union {
  66. u8 secure_channel_id[8];
  67. sci_t sci;
  68. };
  69. __be32 pn;
  70. };
  71. /**
  72. * struct macsec_key - SA key
  73. * @id: user-provided key identifier
  74. * @tfm: crypto struct, key storage
  75. */
  76. struct macsec_key {
  77. u8 id[MACSEC_KEYID_LEN];
  78. struct crypto_aead *tfm;
  79. };
  80. struct macsec_rx_sc_stats {
  81. __u64 InOctetsValidated;
  82. __u64 InOctetsDecrypted;
  83. __u64 InPktsUnchecked;
  84. __u64 InPktsDelayed;
  85. __u64 InPktsOK;
  86. __u64 InPktsInvalid;
  87. __u64 InPktsLate;
  88. __u64 InPktsNotValid;
  89. __u64 InPktsNotUsingSA;
  90. __u64 InPktsUnusedSA;
  91. };
  92. struct macsec_rx_sa_stats {
  93. __u32 InPktsOK;
  94. __u32 InPktsInvalid;
  95. __u32 InPktsNotValid;
  96. __u32 InPktsNotUsingSA;
  97. __u32 InPktsUnusedSA;
  98. };
  99. struct macsec_tx_sa_stats {
  100. __u32 OutPktsProtected;
  101. __u32 OutPktsEncrypted;
  102. };
  103. struct macsec_tx_sc_stats {
  104. __u64 OutPktsProtected;
  105. __u64 OutPktsEncrypted;
  106. __u64 OutOctetsProtected;
  107. __u64 OutOctetsEncrypted;
  108. };
  109. struct macsec_dev_stats {
  110. __u64 OutPktsUntagged;
  111. __u64 InPktsUntagged;
  112. __u64 OutPktsTooLong;
  113. __u64 InPktsNoTag;
  114. __u64 InPktsBadTag;
  115. __u64 InPktsUnknownSCI;
  116. __u64 InPktsNoSCI;
  117. __u64 InPktsOverrun;
  118. };
  119. /**
  120. * struct macsec_rx_sa - receive secure association
  121. * @active:
  122. * @next_pn: packet number expected for the next packet
  123. * @lock: protects next_pn manipulations
  124. * @key: key structure
  125. * @stats: per-SA stats
  126. */
  127. struct macsec_rx_sa {
  128. struct macsec_key key;
  129. spinlock_t lock;
  130. u32 next_pn;
  131. refcount_t refcnt;
  132. bool active;
  133. struct macsec_rx_sa_stats __percpu *stats;
  134. struct macsec_rx_sc *sc;
  135. struct rcu_head rcu;
  136. };
  137. struct pcpu_rx_sc_stats {
  138. struct macsec_rx_sc_stats stats;
  139. struct u64_stats_sync syncp;
  140. };
  141. /**
  142. * struct macsec_rx_sc - receive secure channel
  143. * @sci: secure channel identifier for this SC
  144. * @active: channel is active
  145. * @sa: array of secure associations
  146. * @stats: per-SC stats
  147. */
  148. struct macsec_rx_sc {
  149. struct macsec_rx_sc __rcu *next;
  150. sci_t sci;
  151. bool active;
  152. struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
  153. struct pcpu_rx_sc_stats __percpu *stats;
  154. refcount_t refcnt;
  155. struct rcu_head rcu_head;
  156. };
  157. /**
  158. * struct macsec_tx_sa - transmit secure association
  159. * @active:
  160. * @next_pn: packet number to use for the next packet
  161. * @lock: protects next_pn manipulations
  162. * @key: key structure
  163. * @stats: per-SA stats
  164. */
  165. struct macsec_tx_sa {
  166. struct macsec_key key;
  167. spinlock_t lock;
  168. u32 next_pn;
  169. refcount_t refcnt;
  170. bool active;
  171. struct macsec_tx_sa_stats __percpu *stats;
  172. struct rcu_head rcu;
  173. };
  174. struct pcpu_tx_sc_stats {
  175. struct macsec_tx_sc_stats stats;
  176. struct u64_stats_sync syncp;
  177. };
  178. /**
  179. * struct macsec_tx_sc - transmit secure channel
  180. * @active:
  181. * @encoding_sa: association number of the SA currently in use
  182. * @encrypt: encrypt packets on transmit, or authenticate only
  183. * @send_sci: always include the SCI in the SecTAG
  184. * @end_station:
  185. * @scb: single copy broadcast flag
  186. * @sa: array of secure associations
  187. * @stats: stats for this TXSC
  188. */
  189. struct macsec_tx_sc {
  190. bool active;
  191. u8 encoding_sa;
  192. bool encrypt;
  193. bool send_sci;
  194. bool end_station;
  195. bool scb;
  196. struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
  197. struct pcpu_tx_sc_stats __percpu *stats;
  198. };
  199. #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
  200. /**
  201. * struct macsec_secy - MACsec Security Entity
  202. * @netdev: netdevice for this SecY
  203. * @n_rx_sc: number of receive secure channels configured on this SecY
  204. * @sci: secure channel identifier used for tx
  205. * @key_len: length of keys used by the cipher suite
  206. * @icv_len: length of ICV used by the cipher suite
  207. * @validate_frames: validation mode
  208. * @operational: MAC_Operational flag
  209. * @protect_frames: enable protection for this SecY
  210. * @replay_protect: enable packet number checks on receive
  211. * @replay_window: size of the replay window
  212. * @tx_sc: transmit secure channel
  213. * @rx_sc: linked list of receive secure channels
  214. */
  215. struct macsec_secy {
  216. struct net_device *netdev;
  217. unsigned int n_rx_sc;
  218. sci_t sci;
  219. u16 key_len;
  220. u16 icv_len;
  221. enum macsec_validation_type validate_frames;
  222. bool operational;
  223. bool protect_frames;
  224. bool replay_protect;
  225. u32 replay_window;
  226. struct macsec_tx_sc tx_sc;
  227. struct macsec_rx_sc __rcu *rx_sc;
  228. };
  229. struct pcpu_secy_stats {
  230. struct macsec_dev_stats stats;
  231. struct u64_stats_sync syncp;
  232. };
  233. /**
  234. * struct macsec_dev - private data
  235. * @secy: SecY config
  236. * @real_dev: pointer to underlying netdevice
  237. * @stats: MACsec device stats
  238. * @secys: linked list of SecY's on the underlying device
  239. */
  240. struct macsec_dev {
  241. struct macsec_secy secy;
  242. struct net_device *real_dev;
  243. struct pcpu_secy_stats __percpu *stats;
  244. struct list_head secys;
  245. struct gro_cells gro_cells;
  246. unsigned int nest_level;
  247. };
  248. /**
  249. * struct macsec_rxh_data - rx_handler private argument
  250. * @secys: linked list of SecY's on this underlying device
  251. */
  252. struct macsec_rxh_data {
  253. struct list_head secys;
  254. };
  255. static struct macsec_dev *macsec_priv(const struct net_device *dev)
  256. {
  257. return (struct macsec_dev *)netdev_priv(dev);
  258. }
  259. static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
  260. {
  261. return rcu_dereference_bh(dev->rx_handler_data);
  262. }
  263. static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
  264. {
  265. return rtnl_dereference(dev->rx_handler_data);
  266. }
  267. struct macsec_cb {
  268. struct aead_request *req;
  269. union {
  270. struct macsec_tx_sa *tx_sa;
  271. struct macsec_rx_sa *rx_sa;
  272. };
  273. u8 assoc_num;
  274. bool valid;
  275. bool has_sci;
  276. };
  277. static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
  278. {
  279. struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
  280. if (!sa || !sa->active)
  281. return NULL;
  282. if (!refcount_inc_not_zero(&sa->refcnt))
  283. return NULL;
  284. return sa;
  285. }
  286. static void free_rx_sc_rcu(struct rcu_head *head)
  287. {
  288. struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
  289. free_percpu(rx_sc->stats);
  290. kfree(rx_sc);
  291. }
  292. static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
  293. {
  294. return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
  295. }
  296. static void macsec_rxsc_put(struct macsec_rx_sc *sc)
  297. {
  298. if (refcount_dec_and_test(&sc->refcnt))
  299. call_rcu(&sc->rcu_head, free_rx_sc_rcu);
  300. }
  301. static void free_rxsa(struct rcu_head *head)
  302. {
  303. struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
  304. crypto_free_aead(sa->key.tfm);
  305. free_percpu(sa->stats);
  306. kfree(sa);
  307. }
  308. static void macsec_rxsa_put(struct macsec_rx_sa *sa)
  309. {
  310. if (refcount_dec_and_test(&sa->refcnt))
  311. call_rcu(&sa->rcu, free_rxsa);
  312. }
  313. static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
  314. {
  315. struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
  316. if (!sa || !sa->active)
  317. return NULL;
  318. if (!refcount_inc_not_zero(&sa->refcnt))
  319. return NULL;
  320. return sa;
  321. }
  322. static void free_txsa(struct rcu_head *head)
  323. {
  324. struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
  325. crypto_free_aead(sa->key.tfm);
  326. free_percpu(sa->stats);
  327. kfree(sa);
  328. }
  329. static void macsec_txsa_put(struct macsec_tx_sa *sa)
  330. {
  331. if (refcount_dec_and_test(&sa->refcnt))
  332. call_rcu(&sa->rcu, free_txsa);
  333. }
  334. static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
  335. {
  336. BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
  337. return (struct macsec_cb *)skb->cb;
  338. }
  339. #define MACSEC_PORT_ES (htons(0x0001))
  340. #define MACSEC_PORT_SCB (0x0000)
  341. #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
  342. #define MACSEC_GCM_AES_128_SAK_LEN 16
  343. #define MACSEC_GCM_AES_256_SAK_LEN 32
  344. #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
  345. #define DEFAULT_SEND_SCI true
  346. #define DEFAULT_ENCRYPT false
  347. #define DEFAULT_ENCODING_SA 0
  348. static bool send_sci(const struct macsec_secy *secy)
  349. {
  350. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  351. return tx_sc->send_sci ||
  352. (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
  353. }
  354. static sci_t make_sci(u8 *addr, __be16 port)
  355. {
  356. sci_t sci;
  357. memcpy(&sci, addr, ETH_ALEN);
  358. memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
  359. return sci;
  360. }
  361. static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
  362. {
  363. sci_t sci;
  364. if (sci_present)
  365. memcpy(&sci, hdr->secure_channel_id,
  366. sizeof(hdr->secure_channel_id));
  367. else
  368. sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
  369. return sci;
  370. }
  371. static unsigned int macsec_sectag_len(bool sci_present)
  372. {
  373. return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
  374. }
  375. static unsigned int macsec_hdr_len(bool sci_present)
  376. {
  377. return macsec_sectag_len(sci_present) + ETH_HLEN;
  378. }
  379. static unsigned int macsec_extra_len(bool sci_present)
  380. {
  381. return macsec_sectag_len(sci_present) + sizeof(__be16);
  382. }
  383. /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
  384. static void macsec_fill_sectag(struct macsec_eth_header *h,
  385. const struct macsec_secy *secy, u32 pn,
  386. bool sci_present)
  387. {
  388. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  389. memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
  390. h->eth.h_proto = htons(ETH_P_MACSEC);
  391. if (sci_present) {
  392. h->tci_an |= MACSEC_TCI_SC;
  393. memcpy(&h->secure_channel_id, &secy->sci,
  394. sizeof(h->secure_channel_id));
  395. } else {
  396. if (tx_sc->end_station)
  397. h->tci_an |= MACSEC_TCI_ES;
  398. if (tx_sc->scb)
  399. h->tci_an |= MACSEC_TCI_SCB;
  400. }
  401. h->packet_number = htonl(pn);
  402. /* with GCM, C/E clear for !encrypt, both set for encrypt */
  403. if (tx_sc->encrypt)
  404. h->tci_an |= MACSEC_TCI_CONFID;
  405. else if (secy->icv_len != DEFAULT_ICV_LEN)
  406. h->tci_an |= MACSEC_TCI_C;
  407. h->tci_an |= tx_sc->encoding_sa;
  408. }
  409. static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
  410. {
  411. if (data_len < MIN_NON_SHORT_LEN)
  412. h->short_length = data_len;
  413. }
  414. /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
  415. static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
  416. {
  417. struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
  418. int len = skb->len - 2 * ETH_ALEN;
  419. int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
  420. /* a) It comprises at least 17 octets */
  421. if (skb->len <= 16)
  422. return false;
  423. /* b) MACsec EtherType: already checked */
  424. /* c) V bit is clear */
  425. if (h->tci_an & MACSEC_TCI_VERSION)
  426. return false;
  427. /* d) ES or SCB => !SC */
  428. if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
  429. (h->tci_an & MACSEC_TCI_SC))
  430. return false;
  431. /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
  432. if (h->unused)
  433. return false;
  434. /* rx.pn != 0 (figure 10-5) */
  435. if (!h->packet_number)
  436. return false;
  437. /* length check, f) g) h) i) */
  438. if (h->short_length)
  439. return len == extra_len + h->short_length;
  440. return len >= extra_len + MIN_NON_SHORT_LEN;
  441. }
  442. #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
  443. #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
  444. static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
  445. {
  446. struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
  447. gcm_iv->sci = sci;
  448. gcm_iv->pn = htonl(pn);
  449. }
  450. static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
  451. {
  452. return (struct macsec_eth_header *)skb_mac_header(skb);
  453. }
  454. static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
  455. {
  456. u32 pn;
  457. spin_lock_bh(&tx_sa->lock);
  458. pn = tx_sa->next_pn;
  459. tx_sa->next_pn++;
  460. if (tx_sa->next_pn == 0) {
  461. pr_debug("PN wrapped, transitioning to !oper\n");
  462. tx_sa->active = false;
  463. if (secy->protect_frames)
  464. secy->operational = false;
  465. }
  466. spin_unlock_bh(&tx_sa->lock);
  467. return pn;
  468. }
  469. static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
  470. {
  471. struct macsec_dev *macsec = netdev_priv(dev);
  472. skb->dev = macsec->real_dev;
  473. skb_reset_mac_header(skb);
  474. skb->protocol = eth_hdr(skb)->h_proto;
  475. }
  476. static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
  477. struct macsec_tx_sa *tx_sa)
  478. {
  479. struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
  480. u64_stats_update_begin(&txsc_stats->syncp);
  481. if (tx_sc->encrypt) {
  482. txsc_stats->stats.OutOctetsEncrypted += skb->len;
  483. txsc_stats->stats.OutPktsEncrypted++;
  484. this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
  485. } else {
  486. txsc_stats->stats.OutOctetsProtected += skb->len;
  487. txsc_stats->stats.OutPktsProtected++;
  488. this_cpu_inc(tx_sa->stats->OutPktsProtected);
  489. }
  490. u64_stats_update_end(&txsc_stats->syncp);
  491. }
  492. static void count_tx(struct net_device *dev, int ret, int len)
  493. {
  494. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  495. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  496. u64_stats_update_begin(&stats->syncp);
  497. stats->tx_packets++;
  498. stats->tx_bytes += len;
  499. u64_stats_update_end(&stats->syncp);
  500. }
  501. }
  502. static void macsec_encrypt_done(struct crypto_async_request *base, int err)
  503. {
  504. struct sk_buff *skb = base->data;
  505. struct net_device *dev = skb->dev;
  506. struct macsec_dev *macsec = macsec_priv(dev);
  507. struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
  508. int len, ret;
  509. aead_request_free(macsec_skb_cb(skb)->req);
  510. rcu_read_lock_bh();
  511. macsec_encrypt_finish(skb, dev);
  512. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  513. len = skb->len;
  514. ret = dev_queue_xmit(skb);
  515. count_tx(dev, ret, len);
  516. rcu_read_unlock_bh();
  517. macsec_txsa_put(sa);
  518. dev_put(dev);
  519. }
  520. static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
  521. unsigned char **iv,
  522. struct scatterlist **sg,
  523. int num_frags)
  524. {
  525. size_t size, iv_offset, sg_offset;
  526. struct aead_request *req;
  527. void *tmp;
  528. size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
  529. iv_offset = size;
  530. size += GCM_AES_IV_LEN;
  531. size = ALIGN(size, __alignof__(struct scatterlist));
  532. sg_offset = size;
  533. size += sizeof(struct scatterlist) * num_frags;
  534. tmp = kmalloc(size, GFP_ATOMIC);
  535. if (!tmp)
  536. return NULL;
  537. *iv = (unsigned char *)(tmp + iv_offset);
  538. *sg = (struct scatterlist *)(tmp + sg_offset);
  539. req = tmp;
  540. aead_request_set_tfm(req, tfm);
  541. return req;
  542. }
  543. static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
  544. struct net_device *dev)
  545. {
  546. int ret;
  547. struct scatterlist *sg;
  548. struct sk_buff *trailer;
  549. unsigned char *iv;
  550. struct ethhdr *eth;
  551. struct macsec_eth_header *hh;
  552. size_t unprotected_len;
  553. struct aead_request *req;
  554. struct macsec_secy *secy;
  555. struct macsec_tx_sc *tx_sc;
  556. struct macsec_tx_sa *tx_sa;
  557. struct macsec_dev *macsec = macsec_priv(dev);
  558. bool sci_present;
  559. u32 pn;
  560. secy = &macsec->secy;
  561. tx_sc = &secy->tx_sc;
  562. /* 10.5.1 TX SA assignment */
  563. tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
  564. if (!tx_sa) {
  565. secy->operational = false;
  566. kfree_skb(skb);
  567. return ERR_PTR(-EINVAL);
  568. }
  569. if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
  570. skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
  571. struct sk_buff *nskb = skb_copy_expand(skb,
  572. MACSEC_NEEDED_HEADROOM,
  573. MACSEC_NEEDED_TAILROOM,
  574. GFP_ATOMIC);
  575. if (likely(nskb)) {
  576. consume_skb(skb);
  577. skb = nskb;
  578. } else {
  579. macsec_txsa_put(tx_sa);
  580. kfree_skb(skb);
  581. return ERR_PTR(-ENOMEM);
  582. }
  583. } else {
  584. skb = skb_unshare(skb, GFP_ATOMIC);
  585. if (!skb) {
  586. macsec_txsa_put(tx_sa);
  587. return ERR_PTR(-ENOMEM);
  588. }
  589. }
  590. unprotected_len = skb->len;
  591. eth = eth_hdr(skb);
  592. sci_present = send_sci(secy);
  593. hh = skb_push(skb, macsec_extra_len(sci_present));
  594. memmove(hh, eth, 2 * ETH_ALEN);
  595. pn = tx_sa_update_pn(tx_sa, secy);
  596. if (pn == 0) {
  597. macsec_txsa_put(tx_sa);
  598. kfree_skb(skb);
  599. return ERR_PTR(-ENOLINK);
  600. }
  601. macsec_fill_sectag(hh, secy, pn, sci_present);
  602. macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
  603. skb_put(skb, secy->icv_len);
  604. if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
  605. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  606. u64_stats_update_begin(&secy_stats->syncp);
  607. secy_stats->stats.OutPktsTooLong++;
  608. u64_stats_update_end(&secy_stats->syncp);
  609. macsec_txsa_put(tx_sa);
  610. kfree_skb(skb);
  611. return ERR_PTR(-EINVAL);
  612. }
  613. ret = skb_cow_data(skb, 0, &trailer);
  614. if (unlikely(ret < 0)) {
  615. macsec_txsa_put(tx_sa);
  616. kfree_skb(skb);
  617. return ERR_PTR(ret);
  618. }
  619. req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
  620. if (!req) {
  621. macsec_txsa_put(tx_sa);
  622. kfree_skb(skb);
  623. return ERR_PTR(-ENOMEM);
  624. }
  625. macsec_fill_iv(iv, secy->sci, pn);
  626. sg_init_table(sg, ret);
  627. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  628. if (unlikely(ret < 0)) {
  629. aead_request_free(req);
  630. macsec_txsa_put(tx_sa);
  631. kfree_skb(skb);
  632. return ERR_PTR(ret);
  633. }
  634. if (tx_sc->encrypt) {
  635. int len = skb->len - macsec_hdr_len(sci_present) -
  636. secy->icv_len;
  637. aead_request_set_crypt(req, sg, sg, len, iv);
  638. aead_request_set_ad(req, macsec_hdr_len(sci_present));
  639. } else {
  640. aead_request_set_crypt(req, sg, sg, 0, iv);
  641. aead_request_set_ad(req, skb->len - secy->icv_len);
  642. }
  643. macsec_skb_cb(skb)->req = req;
  644. macsec_skb_cb(skb)->tx_sa = tx_sa;
  645. aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
  646. dev_hold(skb->dev);
  647. ret = crypto_aead_encrypt(req);
  648. if (ret == -EINPROGRESS) {
  649. return ERR_PTR(ret);
  650. } else if (ret != 0) {
  651. dev_put(skb->dev);
  652. kfree_skb(skb);
  653. aead_request_free(req);
  654. macsec_txsa_put(tx_sa);
  655. return ERR_PTR(-EINVAL);
  656. }
  657. dev_put(skb->dev);
  658. aead_request_free(req);
  659. macsec_txsa_put(tx_sa);
  660. return skb;
  661. }
  662. static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
  663. {
  664. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  665. struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
  666. struct macsec_eth_header *hdr = macsec_ethhdr(skb);
  667. u32 lowest_pn = 0;
  668. spin_lock(&rx_sa->lock);
  669. if (rx_sa->next_pn >= secy->replay_window)
  670. lowest_pn = rx_sa->next_pn - secy->replay_window;
  671. /* Now perform replay protection check again
  672. * (see IEEE 802.1AE-2006 figure 10-5)
  673. */
  674. if (secy->replay_protect && pn < lowest_pn) {
  675. spin_unlock(&rx_sa->lock);
  676. u64_stats_update_begin(&rxsc_stats->syncp);
  677. rxsc_stats->stats.InPktsLate++;
  678. u64_stats_update_end(&rxsc_stats->syncp);
  679. return false;
  680. }
  681. if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
  682. u64_stats_update_begin(&rxsc_stats->syncp);
  683. if (hdr->tci_an & MACSEC_TCI_E)
  684. rxsc_stats->stats.InOctetsDecrypted += skb->len;
  685. else
  686. rxsc_stats->stats.InOctetsValidated += skb->len;
  687. u64_stats_update_end(&rxsc_stats->syncp);
  688. }
  689. if (!macsec_skb_cb(skb)->valid) {
  690. spin_unlock(&rx_sa->lock);
  691. /* 10.6.5 */
  692. if (hdr->tci_an & MACSEC_TCI_C ||
  693. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  694. u64_stats_update_begin(&rxsc_stats->syncp);
  695. rxsc_stats->stats.InPktsNotValid++;
  696. u64_stats_update_end(&rxsc_stats->syncp);
  697. return false;
  698. }
  699. u64_stats_update_begin(&rxsc_stats->syncp);
  700. if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
  701. rxsc_stats->stats.InPktsInvalid++;
  702. this_cpu_inc(rx_sa->stats->InPktsInvalid);
  703. } else if (pn < lowest_pn) {
  704. rxsc_stats->stats.InPktsDelayed++;
  705. } else {
  706. rxsc_stats->stats.InPktsUnchecked++;
  707. }
  708. u64_stats_update_end(&rxsc_stats->syncp);
  709. } else {
  710. u64_stats_update_begin(&rxsc_stats->syncp);
  711. if (pn < lowest_pn) {
  712. rxsc_stats->stats.InPktsDelayed++;
  713. } else {
  714. rxsc_stats->stats.InPktsOK++;
  715. this_cpu_inc(rx_sa->stats->InPktsOK);
  716. }
  717. u64_stats_update_end(&rxsc_stats->syncp);
  718. if (pn >= rx_sa->next_pn)
  719. rx_sa->next_pn = pn + 1;
  720. spin_unlock(&rx_sa->lock);
  721. }
  722. return true;
  723. }
  724. static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
  725. {
  726. skb->pkt_type = PACKET_HOST;
  727. skb->protocol = eth_type_trans(skb, dev);
  728. skb_reset_network_header(skb);
  729. if (!skb_transport_header_was_set(skb))
  730. skb_reset_transport_header(skb);
  731. skb_reset_mac_len(skb);
  732. }
  733. static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
  734. {
  735. skb->ip_summed = CHECKSUM_NONE;
  736. memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
  737. skb_pull(skb, hdr_len);
  738. pskb_trim_unique(skb, skb->len - icv_len);
  739. }
  740. static void count_rx(struct net_device *dev, int len)
  741. {
  742. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  743. u64_stats_update_begin(&stats->syncp);
  744. stats->rx_packets++;
  745. stats->rx_bytes += len;
  746. u64_stats_update_end(&stats->syncp);
  747. }
  748. static void macsec_decrypt_done(struct crypto_async_request *base, int err)
  749. {
  750. struct sk_buff *skb = base->data;
  751. struct net_device *dev = skb->dev;
  752. struct macsec_dev *macsec = macsec_priv(dev);
  753. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  754. struct macsec_rx_sc *rx_sc = rx_sa->sc;
  755. int len;
  756. u32 pn;
  757. aead_request_free(macsec_skb_cb(skb)->req);
  758. if (!err)
  759. macsec_skb_cb(skb)->valid = true;
  760. rcu_read_lock_bh();
  761. pn = ntohl(macsec_ethhdr(skb)->packet_number);
  762. if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
  763. rcu_read_unlock_bh();
  764. kfree_skb(skb);
  765. goto out;
  766. }
  767. macsec_finalize_skb(skb, macsec->secy.icv_len,
  768. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  769. macsec_reset_skb(skb, macsec->secy.netdev);
  770. len = skb->len;
  771. if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
  772. count_rx(dev, len);
  773. rcu_read_unlock_bh();
  774. out:
  775. macsec_rxsa_put(rx_sa);
  776. macsec_rxsc_put(rx_sc);
  777. dev_put(dev);
  778. }
  779. static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
  780. struct net_device *dev,
  781. struct macsec_rx_sa *rx_sa,
  782. sci_t sci,
  783. struct macsec_secy *secy)
  784. {
  785. int ret;
  786. struct scatterlist *sg;
  787. struct sk_buff *trailer;
  788. unsigned char *iv;
  789. struct aead_request *req;
  790. struct macsec_eth_header *hdr;
  791. u16 icv_len = secy->icv_len;
  792. macsec_skb_cb(skb)->valid = false;
  793. skb = skb_share_check(skb, GFP_ATOMIC);
  794. if (!skb)
  795. return ERR_PTR(-ENOMEM);
  796. ret = skb_cow_data(skb, 0, &trailer);
  797. if (unlikely(ret < 0)) {
  798. kfree_skb(skb);
  799. return ERR_PTR(ret);
  800. }
  801. req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
  802. if (!req) {
  803. kfree_skb(skb);
  804. return ERR_PTR(-ENOMEM);
  805. }
  806. hdr = (struct macsec_eth_header *)skb->data;
  807. macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
  808. sg_init_table(sg, ret);
  809. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  810. if (unlikely(ret < 0)) {
  811. aead_request_free(req);
  812. kfree_skb(skb);
  813. return ERR_PTR(ret);
  814. }
  815. if (hdr->tci_an & MACSEC_TCI_E) {
  816. /* confidentiality: ethernet + macsec header
  817. * authenticated, encrypted payload
  818. */
  819. int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
  820. aead_request_set_crypt(req, sg, sg, len, iv);
  821. aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
  822. skb = skb_unshare(skb, GFP_ATOMIC);
  823. if (!skb) {
  824. aead_request_free(req);
  825. return ERR_PTR(-ENOMEM);
  826. }
  827. } else {
  828. /* integrity only: all headers + data authenticated */
  829. aead_request_set_crypt(req, sg, sg, icv_len, iv);
  830. aead_request_set_ad(req, skb->len - icv_len);
  831. }
  832. macsec_skb_cb(skb)->req = req;
  833. skb->dev = dev;
  834. aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
  835. dev_hold(dev);
  836. ret = crypto_aead_decrypt(req);
  837. if (ret == -EINPROGRESS) {
  838. return ERR_PTR(ret);
  839. } else if (ret != 0) {
  840. /* decryption/authentication failed
  841. * 10.6 if validateFrames is disabled, deliver anyway
  842. */
  843. if (ret != -EBADMSG) {
  844. kfree_skb(skb);
  845. skb = ERR_PTR(ret);
  846. }
  847. } else {
  848. macsec_skb_cb(skb)->valid = true;
  849. }
  850. dev_put(dev);
  851. aead_request_free(req);
  852. return skb;
  853. }
  854. static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
  855. {
  856. struct macsec_rx_sc *rx_sc;
  857. for_each_rxsc(secy, rx_sc) {
  858. if (rx_sc->sci == sci)
  859. return rx_sc;
  860. }
  861. return NULL;
  862. }
  863. static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
  864. {
  865. struct macsec_rx_sc *rx_sc;
  866. for_each_rxsc_rtnl(secy, rx_sc) {
  867. if (rx_sc->sci == sci)
  868. return rx_sc;
  869. }
  870. return NULL;
  871. }
  872. static void handle_not_macsec(struct sk_buff *skb)
  873. {
  874. struct macsec_rxh_data *rxd;
  875. struct macsec_dev *macsec;
  876. rcu_read_lock();
  877. rxd = macsec_data_rcu(skb->dev);
  878. /* 10.6 If the management control validateFrames is not
  879. * Strict, frames without a SecTAG are received, counted, and
  880. * delivered to the Controlled Port
  881. */
  882. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  883. struct sk_buff *nskb;
  884. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  885. if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  886. u64_stats_update_begin(&secy_stats->syncp);
  887. secy_stats->stats.InPktsNoTag++;
  888. u64_stats_update_end(&secy_stats->syncp);
  889. continue;
  890. }
  891. /* deliver on this port */
  892. nskb = skb_clone(skb, GFP_ATOMIC);
  893. if (!nskb)
  894. break;
  895. nskb->dev = macsec->secy.netdev;
  896. if (netif_rx(nskb) == NET_RX_SUCCESS) {
  897. u64_stats_update_begin(&secy_stats->syncp);
  898. secy_stats->stats.InPktsUntagged++;
  899. u64_stats_update_end(&secy_stats->syncp);
  900. }
  901. }
  902. rcu_read_unlock();
  903. }
  904. static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
  905. {
  906. struct sk_buff *skb = *pskb;
  907. struct net_device *dev = skb->dev;
  908. struct macsec_eth_header *hdr;
  909. struct macsec_secy *secy = NULL;
  910. struct macsec_rx_sc *rx_sc;
  911. struct macsec_rx_sa *rx_sa;
  912. struct macsec_rxh_data *rxd;
  913. struct macsec_dev *macsec;
  914. sci_t sci;
  915. u32 pn;
  916. bool cbit;
  917. struct pcpu_rx_sc_stats *rxsc_stats;
  918. struct pcpu_secy_stats *secy_stats;
  919. bool pulled_sci;
  920. int ret;
  921. if (skb_headroom(skb) < ETH_HLEN)
  922. goto drop_direct;
  923. hdr = macsec_ethhdr(skb);
  924. if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
  925. handle_not_macsec(skb);
  926. /* and deliver to the uncontrolled port */
  927. return RX_HANDLER_PASS;
  928. }
  929. skb = skb_unshare(skb, GFP_ATOMIC);
  930. *pskb = skb;
  931. if (!skb)
  932. return RX_HANDLER_CONSUMED;
  933. pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
  934. if (!pulled_sci) {
  935. if (!pskb_may_pull(skb, macsec_extra_len(false)))
  936. goto drop_direct;
  937. }
  938. hdr = macsec_ethhdr(skb);
  939. /* Frames with a SecTAG that has the TCI E bit set but the C
  940. * bit clear are discarded, as this reserved encoding is used
  941. * to identify frames with a SecTAG that are not to be
  942. * delivered to the Controlled Port.
  943. */
  944. if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
  945. return RX_HANDLER_PASS;
  946. /* now, pull the extra length */
  947. if (hdr->tci_an & MACSEC_TCI_SC) {
  948. if (!pulled_sci)
  949. goto drop_direct;
  950. }
  951. /* ethernet header is part of crypto processing */
  952. skb_push(skb, ETH_HLEN);
  953. macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
  954. macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
  955. sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
  956. rcu_read_lock();
  957. rxd = macsec_data_rcu(skb->dev);
  958. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  959. struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
  960. sc = sc ? macsec_rxsc_get(sc) : NULL;
  961. if (sc) {
  962. secy = &macsec->secy;
  963. rx_sc = sc;
  964. break;
  965. }
  966. }
  967. if (!secy)
  968. goto nosci;
  969. dev = secy->netdev;
  970. macsec = macsec_priv(dev);
  971. secy_stats = this_cpu_ptr(macsec->stats);
  972. rxsc_stats = this_cpu_ptr(rx_sc->stats);
  973. if (!macsec_validate_skb(skb, secy->icv_len)) {
  974. u64_stats_update_begin(&secy_stats->syncp);
  975. secy_stats->stats.InPktsBadTag++;
  976. u64_stats_update_end(&secy_stats->syncp);
  977. goto drop_nosa;
  978. }
  979. rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
  980. if (!rx_sa) {
  981. /* 10.6.1 if the SA is not in use */
  982. /* If validateFrames is Strict or the C bit in the
  983. * SecTAG is set, discard
  984. */
  985. if (hdr->tci_an & MACSEC_TCI_C ||
  986. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  987. u64_stats_update_begin(&rxsc_stats->syncp);
  988. rxsc_stats->stats.InPktsNotUsingSA++;
  989. u64_stats_update_end(&rxsc_stats->syncp);
  990. goto drop_nosa;
  991. }
  992. /* not Strict, the frame (with the SecTAG and ICV
  993. * removed) is delivered to the Controlled Port.
  994. */
  995. u64_stats_update_begin(&rxsc_stats->syncp);
  996. rxsc_stats->stats.InPktsUnusedSA++;
  997. u64_stats_update_end(&rxsc_stats->syncp);
  998. goto deliver;
  999. }
  1000. /* First, PN check to avoid decrypting obviously wrong packets */
  1001. pn = ntohl(hdr->packet_number);
  1002. if (secy->replay_protect) {
  1003. bool late;
  1004. spin_lock(&rx_sa->lock);
  1005. late = rx_sa->next_pn >= secy->replay_window &&
  1006. pn < (rx_sa->next_pn - secy->replay_window);
  1007. spin_unlock(&rx_sa->lock);
  1008. if (late) {
  1009. u64_stats_update_begin(&rxsc_stats->syncp);
  1010. rxsc_stats->stats.InPktsLate++;
  1011. u64_stats_update_end(&rxsc_stats->syncp);
  1012. goto drop;
  1013. }
  1014. }
  1015. macsec_skb_cb(skb)->rx_sa = rx_sa;
  1016. /* Disabled && !changed text => skip validation */
  1017. if (hdr->tci_an & MACSEC_TCI_C ||
  1018. secy->validate_frames != MACSEC_VALIDATE_DISABLED)
  1019. skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
  1020. if (IS_ERR(skb)) {
  1021. /* the decrypt callback needs the reference */
  1022. if (PTR_ERR(skb) != -EINPROGRESS) {
  1023. macsec_rxsa_put(rx_sa);
  1024. macsec_rxsc_put(rx_sc);
  1025. }
  1026. rcu_read_unlock();
  1027. *pskb = NULL;
  1028. return RX_HANDLER_CONSUMED;
  1029. }
  1030. if (!macsec_post_decrypt(skb, secy, pn))
  1031. goto drop;
  1032. deliver:
  1033. macsec_finalize_skb(skb, secy->icv_len,
  1034. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1035. macsec_reset_skb(skb, secy->netdev);
  1036. if (rx_sa)
  1037. macsec_rxsa_put(rx_sa);
  1038. macsec_rxsc_put(rx_sc);
  1039. skb_orphan(skb);
  1040. ret = gro_cells_receive(&macsec->gro_cells, skb);
  1041. if (ret == NET_RX_SUCCESS)
  1042. count_rx(dev, skb->len);
  1043. else
  1044. macsec->secy.netdev->stats.rx_dropped++;
  1045. rcu_read_unlock();
  1046. *pskb = NULL;
  1047. return RX_HANDLER_CONSUMED;
  1048. drop:
  1049. macsec_rxsa_put(rx_sa);
  1050. drop_nosa:
  1051. macsec_rxsc_put(rx_sc);
  1052. rcu_read_unlock();
  1053. drop_direct:
  1054. kfree_skb(skb);
  1055. *pskb = NULL;
  1056. return RX_HANDLER_CONSUMED;
  1057. nosci:
  1058. /* 10.6.1 if the SC is not found */
  1059. cbit = !!(hdr->tci_an & MACSEC_TCI_C);
  1060. if (!cbit)
  1061. macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
  1062. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1063. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  1064. struct sk_buff *nskb;
  1065. secy_stats = this_cpu_ptr(macsec->stats);
  1066. /* If validateFrames is Strict or the C bit in the
  1067. * SecTAG is set, discard
  1068. */
  1069. if (cbit ||
  1070. macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  1071. u64_stats_update_begin(&secy_stats->syncp);
  1072. secy_stats->stats.InPktsNoSCI++;
  1073. u64_stats_update_end(&secy_stats->syncp);
  1074. continue;
  1075. }
  1076. /* not strict, the frame (with the SecTAG and ICV
  1077. * removed) is delivered to the Controlled Port.
  1078. */
  1079. nskb = skb_clone(skb, GFP_ATOMIC);
  1080. if (!nskb)
  1081. break;
  1082. macsec_reset_skb(nskb, macsec->secy.netdev);
  1083. ret = netif_rx(nskb);
  1084. if (ret == NET_RX_SUCCESS) {
  1085. u64_stats_update_begin(&secy_stats->syncp);
  1086. secy_stats->stats.InPktsUnknownSCI++;
  1087. u64_stats_update_end(&secy_stats->syncp);
  1088. } else {
  1089. macsec->secy.netdev->stats.rx_dropped++;
  1090. }
  1091. }
  1092. rcu_read_unlock();
  1093. *pskb = skb;
  1094. return RX_HANDLER_PASS;
  1095. }
  1096. static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
  1097. {
  1098. struct crypto_aead *tfm;
  1099. int ret;
  1100. tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  1101. if (IS_ERR(tfm))
  1102. return tfm;
  1103. ret = crypto_aead_setkey(tfm, key, key_len);
  1104. if (ret < 0)
  1105. goto fail;
  1106. ret = crypto_aead_setauthsize(tfm, icv_len);
  1107. if (ret < 0)
  1108. goto fail;
  1109. return tfm;
  1110. fail:
  1111. crypto_free_aead(tfm);
  1112. return ERR_PTR(ret);
  1113. }
  1114. static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
  1115. int icv_len)
  1116. {
  1117. rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
  1118. if (!rx_sa->stats)
  1119. return -ENOMEM;
  1120. rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1121. if (IS_ERR(rx_sa->key.tfm)) {
  1122. free_percpu(rx_sa->stats);
  1123. return PTR_ERR(rx_sa->key.tfm);
  1124. }
  1125. rx_sa->active = false;
  1126. rx_sa->next_pn = 1;
  1127. refcount_set(&rx_sa->refcnt, 1);
  1128. spin_lock_init(&rx_sa->lock);
  1129. return 0;
  1130. }
  1131. static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
  1132. {
  1133. rx_sa->active = false;
  1134. macsec_rxsa_put(rx_sa);
  1135. }
  1136. static void free_rx_sc(struct macsec_rx_sc *rx_sc)
  1137. {
  1138. int i;
  1139. for (i = 0; i < MACSEC_NUM_AN; i++) {
  1140. struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
  1141. RCU_INIT_POINTER(rx_sc->sa[i], NULL);
  1142. if (sa)
  1143. clear_rx_sa(sa);
  1144. }
  1145. macsec_rxsc_put(rx_sc);
  1146. }
  1147. static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
  1148. {
  1149. struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
  1150. for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
  1151. rx_sc;
  1152. rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
  1153. if (rx_sc->sci == sci) {
  1154. if (rx_sc->active)
  1155. secy->n_rx_sc--;
  1156. rcu_assign_pointer(*rx_scp, rx_sc->next);
  1157. return rx_sc;
  1158. }
  1159. }
  1160. return NULL;
  1161. }
  1162. static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
  1163. {
  1164. struct macsec_rx_sc *rx_sc;
  1165. struct macsec_dev *macsec;
  1166. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  1167. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  1168. struct macsec_secy *secy;
  1169. list_for_each_entry(macsec, &rxd->secys, secys) {
  1170. if (find_rx_sc_rtnl(&macsec->secy, sci))
  1171. return ERR_PTR(-EEXIST);
  1172. }
  1173. rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
  1174. if (!rx_sc)
  1175. return ERR_PTR(-ENOMEM);
  1176. rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
  1177. if (!rx_sc->stats) {
  1178. kfree(rx_sc);
  1179. return ERR_PTR(-ENOMEM);
  1180. }
  1181. rx_sc->sci = sci;
  1182. rx_sc->active = true;
  1183. refcount_set(&rx_sc->refcnt, 1);
  1184. secy = &macsec_priv(dev)->secy;
  1185. rcu_assign_pointer(rx_sc->next, secy->rx_sc);
  1186. rcu_assign_pointer(secy->rx_sc, rx_sc);
  1187. if (rx_sc->active)
  1188. secy->n_rx_sc++;
  1189. return rx_sc;
  1190. }
  1191. static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
  1192. int icv_len)
  1193. {
  1194. tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
  1195. if (!tx_sa->stats)
  1196. return -ENOMEM;
  1197. tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1198. if (IS_ERR(tx_sa->key.tfm)) {
  1199. free_percpu(tx_sa->stats);
  1200. return PTR_ERR(tx_sa->key.tfm);
  1201. }
  1202. tx_sa->active = false;
  1203. refcount_set(&tx_sa->refcnt, 1);
  1204. spin_lock_init(&tx_sa->lock);
  1205. return 0;
  1206. }
  1207. static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
  1208. {
  1209. tx_sa->active = false;
  1210. macsec_txsa_put(tx_sa);
  1211. }
  1212. static struct genl_family macsec_fam;
  1213. static struct net_device *get_dev_from_nl(struct net *net,
  1214. struct nlattr **attrs)
  1215. {
  1216. int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
  1217. struct net_device *dev;
  1218. dev = __dev_get_by_index(net, ifindex);
  1219. if (!dev)
  1220. return ERR_PTR(-ENODEV);
  1221. if (!netif_is_macsec(dev))
  1222. return ERR_PTR(-ENODEV);
  1223. return dev;
  1224. }
  1225. static sci_t nla_get_sci(const struct nlattr *nla)
  1226. {
  1227. return (__force sci_t)nla_get_u64(nla);
  1228. }
  1229. static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
  1230. int padattr)
  1231. {
  1232. return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
  1233. }
  1234. static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
  1235. struct nlattr **attrs,
  1236. struct nlattr **tb_sa,
  1237. struct net_device **devp,
  1238. struct macsec_secy **secyp,
  1239. struct macsec_tx_sc **scp,
  1240. u8 *assoc_num)
  1241. {
  1242. struct net_device *dev;
  1243. struct macsec_secy *secy;
  1244. struct macsec_tx_sc *tx_sc;
  1245. struct macsec_tx_sa *tx_sa;
  1246. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1247. return ERR_PTR(-EINVAL);
  1248. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1249. dev = get_dev_from_nl(net, attrs);
  1250. if (IS_ERR(dev))
  1251. return ERR_CAST(dev);
  1252. if (*assoc_num >= MACSEC_NUM_AN)
  1253. return ERR_PTR(-EINVAL);
  1254. secy = &macsec_priv(dev)->secy;
  1255. tx_sc = &secy->tx_sc;
  1256. tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
  1257. if (!tx_sa)
  1258. return ERR_PTR(-ENODEV);
  1259. *devp = dev;
  1260. *scp = tx_sc;
  1261. *secyp = secy;
  1262. return tx_sa;
  1263. }
  1264. static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
  1265. struct nlattr **attrs,
  1266. struct nlattr **tb_rxsc,
  1267. struct net_device **devp,
  1268. struct macsec_secy **secyp)
  1269. {
  1270. struct net_device *dev;
  1271. struct macsec_secy *secy;
  1272. struct macsec_rx_sc *rx_sc;
  1273. sci_t sci;
  1274. dev = get_dev_from_nl(net, attrs);
  1275. if (IS_ERR(dev))
  1276. return ERR_CAST(dev);
  1277. secy = &macsec_priv(dev)->secy;
  1278. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1279. return ERR_PTR(-EINVAL);
  1280. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1281. rx_sc = find_rx_sc_rtnl(secy, sci);
  1282. if (!rx_sc)
  1283. return ERR_PTR(-ENODEV);
  1284. *secyp = secy;
  1285. *devp = dev;
  1286. return rx_sc;
  1287. }
  1288. static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
  1289. struct nlattr **attrs,
  1290. struct nlattr **tb_rxsc,
  1291. struct nlattr **tb_sa,
  1292. struct net_device **devp,
  1293. struct macsec_secy **secyp,
  1294. struct macsec_rx_sc **scp,
  1295. u8 *assoc_num)
  1296. {
  1297. struct macsec_rx_sc *rx_sc;
  1298. struct macsec_rx_sa *rx_sa;
  1299. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1300. return ERR_PTR(-EINVAL);
  1301. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1302. if (*assoc_num >= MACSEC_NUM_AN)
  1303. return ERR_PTR(-EINVAL);
  1304. rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
  1305. if (IS_ERR(rx_sc))
  1306. return ERR_CAST(rx_sc);
  1307. rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
  1308. if (!rx_sa)
  1309. return ERR_PTR(-ENODEV);
  1310. *scp = rx_sc;
  1311. return rx_sa;
  1312. }
  1313. static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
  1314. [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
  1315. [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
  1316. [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
  1317. };
  1318. static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
  1319. [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
  1320. [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
  1321. };
  1322. static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
  1323. [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
  1324. [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
  1325. [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
  1326. [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
  1327. .len = MACSEC_KEYID_LEN, },
  1328. [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
  1329. .len = MACSEC_MAX_KEY_LEN, },
  1330. };
  1331. static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
  1332. {
  1333. if (!attrs[MACSEC_ATTR_SA_CONFIG])
  1334. return -EINVAL;
  1335. if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX,
  1336. attrs[MACSEC_ATTR_SA_CONFIG],
  1337. macsec_genl_sa_policy, NULL))
  1338. return -EINVAL;
  1339. return 0;
  1340. }
  1341. static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
  1342. {
  1343. if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
  1344. return -EINVAL;
  1345. if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX,
  1346. attrs[MACSEC_ATTR_RXSC_CONFIG],
  1347. macsec_genl_rxsc_policy, NULL))
  1348. return -EINVAL;
  1349. return 0;
  1350. }
  1351. static bool validate_add_rxsa(struct nlattr **attrs)
  1352. {
  1353. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1354. !attrs[MACSEC_SA_ATTR_KEY] ||
  1355. !attrs[MACSEC_SA_ATTR_KEYID])
  1356. return false;
  1357. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1358. return false;
  1359. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1360. return false;
  1361. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1362. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1363. return false;
  1364. }
  1365. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1366. return false;
  1367. return true;
  1368. }
  1369. static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
  1370. {
  1371. struct net_device *dev;
  1372. struct nlattr **attrs = info->attrs;
  1373. struct macsec_secy *secy;
  1374. struct macsec_rx_sc *rx_sc;
  1375. struct macsec_rx_sa *rx_sa;
  1376. unsigned char assoc_num;
  1377. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1378. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1379. int err;
  1380. if (!attrs[MACSEC_ATTR_IFINDEX])
  1381. return -EINVAL;
  1382. if (parse_sa_config(attrs, tb_sa))
  1383. return -EINVAL;
  1384. if (parse_rxsc_config(attrs, tb_rxsc))
  1385. return -EINVAL;
  1386. if (!validate_add_rxsa(tb_sa))
  1387. return -EINVAL;
  1388. rtnl_lock();
  1389. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1390. if (IS_ERR(rx_sc)) {
  1391. rtnl_unlock();
  1392. return PTR_ERR(rx_sc);
  1393. }
  1394. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1395. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1396. pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
  1397. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1398. rtnl_unlock();
  1399. return -EINVAL;
  1400. }
  1401. rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
  1402. if (rx_sa) {
  1403. rtnl_unlock();
  1404. return -EBUSY;
  1405. }
  1406. rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
  1407. if (!rx_sa) {
  1408. rtnl_unlock();
  1409. return -ENOMEM;
  1410. }
  1411. err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1412. secy->key_len, secy->icv_len);
  1413. if (err < 0) {
  1414. kfree(rx_sa);
  1415. rtnl_unlock();
  1416. return err;
  1417. }
  1418. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1419. spin_lock_bh(&rx_sa->lock);
  1420. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1421. spin_unlock_bh(&rx_sa->lock);
  1422. }
  1423. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1424. rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1425. nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1426. rx_sa->sc = rx_sc;
  1427. rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
  1428. rtnl_unlock();
  1429. return 0;
  1430. }
  1431. static bool validate_add_rxsc(struct nlattr **attrs)
  1432. {
  1433. if (!attrs[MACSEC_RXSC_ATTR_SCI])
  1434. return false;
  1435. if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
  1436. if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
  1437. return false;
  1438. }
  1439. return true;
  1440. }
  1441. static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
  1442. {
  1443. struct net_device *dev;
  1444. sci_t sci = MACSEC_UNDEF_SCI;
  1445. struct nlattr **attrs = info->attrs;
  1446. struct macsec_rx_sc *rx_sc;
  1447. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1448. if (!attrs[MACSEC_ATTR_IFINDEX])
  1449. return -EINVAL;
  1450. if (parse_rxsc_config(attrs, tb_rxsc))
  1451. return -EINVAL;
  1452. if (!validate_add_rxsc(tb_rxsc))
  1453. return -EINVAL;
  1454. rtnl_lock();
  1455. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1456. if (IS_ERR(dev)) {
  1457. rtnl_unlock();
  1458. return PTR_ERR(dev);
  1459. }
  1460. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1461. rx_sc = create_rx_sc(dev, sci);
  1462. if (IS_ERR(rx_sc)) {
  1463. rtnl_unlock();
  1464. return PTR_ERR(rx_sc);
  1465. }
  1466. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
  1467. rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1468. rtnl_unlock();
  1469. return 0;
  1470. }
  1471. static bool validate_add_txsa(struct nlattr **attrs)
  1472. {
  1473. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1474. !attrs[MACSEC_SA_ATTR_PN] ||
  1475. !attrs[MACSEC_SA_ATTR_KEY] ||
  1476. !attrs[MACSEC_SA_ATTR_KEYID])
  1477. return false;
  1478. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1479. return false;
  1480. if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1481. return false;
  1482. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1483. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1484. return false;
  1485. }
  1486. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1487. return false;
  1488. return true;
  1489. }
  1490. static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
  1491. {
  1492. struct net_device *dev;
  1493. struct nlattr **attrs = info->attrs;
  1494. struct macsec_secy *secy;
  1495. struct macsec_tx_sc *tx_sc;
  1496. struct macsec_tx_sa *tx_sa;
  1497. unsigned char assoc_num;
  1498. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1499. int err;
  1500. if (!attrs[MACSEC_ATTR_IFINDEX])
  1501. return -EINVAL;
  1502. if (parse_sa_config(attrs, tb_sa))
  1503. return -EINVAL;
  1504. if (!validate_add_txsa(tb_sa))
  1505. return -EINVAL;
  1506. rtnl_lock();
  1507. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1508. if (IS_ERR(dev)) {
  1509. rtnl_unlock();
  1510. return PTR_ERR(dev);
  1511. }
  1512. secy = &macsec_priv(dev)->secy;
  1513. tx_sc = &secy->tx_sc;
  1514. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1515. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1516. pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
  1517. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1518. rtnl_unlock();
  1519. return -EINVAL;
  1520. }
  1521. tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
  1522. if (tx_sa) {
  1523. rtnl_unlock();
  1524. return -EBUSY;
  1525. }
  1526. tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
  1527. if (!tx_sa) {
  1528. rtnl_unlock();
  1529. return -ENOMEM;
  1530. }
  1531. err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1532. secy->key_len, secy->icv_len);
  1533. if (err < 0) {
  1534. kfree(tx_sa);
  1535. rtnl_unlock();
  1536. return err;
  1537. }
  1538. nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1539. spin_lock_bh(&tx_sa->lock);
  1540. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1541. spin_unlock_bh(&tx_sa->lock);
  1542. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1543. tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1544. if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
  1545. secy->operational = true;
  1546. rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
  1547. rtnl_unlock();
  1548. return 0;
  1549. }
  1550. static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
  1551. {
  1552. struct nlattr **attrs = info->attrs;
  1553. struct net_device *dev;
  1554. struct macsec_secy *secy;
  1555. struct macsec_rx_sc *rx_sc;
  1556. struct macsec_rx_sa *rx_sa;
  1557. u8 assoc_num;
  1558. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1559. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1560. if (!attrs[MACSEC_ATTR_IFINDEX])
  1561. return -EINVAL;
  1562. if (parse_sa_config(attrs, tb_sa))
  1563. return -EINVAL;
  1564. if (parse_rxsc_config(attrs, tb_rxsc))
  1565. return -EINVAL;
  1566. rtnl_lock();
  1567. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1568. &dev, &secy, &rx_sc, &assoc_num);
  1569. if (IS_ERR(rx_sa)) {
  1570. rtnl_unlock();
  1571. return PTR_ERR(rx_sa);
  1572. }
  1573. if (rx_sa->active) {
  1574. rtnl_unlock();
  1575. return -EBUSY;
  1576. }
  1577. RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
  1578. clear_rx_sa(rx_sa);
  1579. rtnl_unlock();
  1580. return 0;
  1581. }
  1582. static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
  1583. {
  1584. struct nlattr **attrs = info->attrs;
  1585. struct net_device *dev;
  1586. struct macsec_secy *secy;
  1587. struct macsec_rx_sc *rx_sc;
  1588. sci_t sci;
  1589. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1590. if (!attrs[MACSEC_ATTR_IFINDEX])
  1591. return -EINVAL;
  1592. if (parse_rxsc_config(attrs, tb_rxsc))
  1593. return -EINVAL;
  1594. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1595. return -EINVAL;
  1596. rtnl_lock();
  1597. dev = get_dev_from_nl(genl_info_net(info), info->attrs);
  1598. if (IS_ERR(dev)) {
  1599. rtnl_unlock();
  1600. return PTR_ERR(dev);
  1601. }
  1602. secy = &macsec_priv(dev)->secy;
  1603. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1604. rx_sc = del_rx_sc(secy, sci);
  1605. if (!rx_sc) {
  1606. rtnl_unlock();
  1607. return -ENODEV;
  1608. }
  1609. free_rx_sc(rx_sc);
  1610. rtnl_unlock();
  1611. return 0;
  1612. }
  1613. static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
  1614. {
  1615. struct nlattr **attrs = info->attrs;
  1616. struct net_device *dev;
  1617. struct macsec_secy *secy;
  1618. struct macsec_tx_sc *tx_sc;
  1619. struct macsec_tx_sa *tx_sa;
  1620. u8 assoc_num;
  1621. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1622. if (!attrs[MACSEC_ATTR_IFINDEX])
  1623. return -EINVAL;
  1624. if (parse_sa_config(attrs, tb_sa))
  1625. return -EINVAL;
  1626. rtnl_lock();
  1627. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1628. &dev, &secy, &tx_sc, &assoc_num);
  1629. if (IS_ERR(tx_sa)) {
  1630. rtnl_unlock();
  1631. return PTR_ERR(tx_sa);
  1632. }
  1633. if (tx_sa->active) {
  1634. rtnl_unlock();
  1635. return -EBUSY;
  1636. }
  1637. RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
  1638. clear_tx_sa(tx_sa);
  1639. rtnl_unlock();
  1640. return 0;
  1641. }
  1642. static bool validate_upd_sa(struct nlattr **attrs)
  1643. {
  1644. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1645. attrs[MACSEC_SA_ATTR_KEY] ||
  1646. attrs[MACSEC_SA_ATTR_KEYID])
  1647. return false;
  1648. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1649. return false;
  1650. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1651. return false;
  1652. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1653. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1654. return false;
  1655. }
  1656. return true;
  1657. }
  1658. static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
  1659. {
  1660. struct nlattr **attrs = info->attrs;
  1661. struct net_device *dev;
  1662. struct macsec_secy *secy;
  1663. struct macsec_tx_sc *tx_sc;
  1664. struct macsec_tx_sa *tx_sa;
  1665. u8 assoc_num;
  1666. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1667. if (!attrs[MACSEC_ATTR_IFINDEX])
  1668. return -EINVAL;
  1669. if (parse_sa_config(attrs, tb_sa))
  1670. return -EINVAL;
  1671. if (!validate_upd_sa(tb_sa))
  1672. return -EINVAL;
  1673. rtnl_lock();
  1674. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1675. &dev, &secy, &tx_sc, &assoc_num);
  1676. if (IS_ERR(tx_sa)) {
  1677. rtnl_unlock();
  1678. return PTR_ERR(tx_sa);
  1679. }
  1680. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1681. spin_lock_bh(&tx_sa->lock);
  1682. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1683. spin_unlock_bh(&tx_sa->lock);
  1684. }
  1685. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1686. tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1687. if (assoc_num == tx_sc->encoding_sa)
  1688. secy->operational = tx_sa->active;
  1689. rtnl_unlock();
  1690. return 0;
  1691. }
  1692. static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
  1693. {
  1694. struct nlattr **attrs = info->attrs;
  1695. struct net_device *dev;
  1696. struct macsec_secy *secy;
  1697. struct macsec_rx_sc *rx_sc;
  1698. struct macsec_rx_sa *rx_sa;
  1699. u8 assoc_num;
  1700. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1701. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1702. if (!attrs[MACSEC_ATTR_IFINDEX])
  1703. return -EINVAL;
  1704. if (parse_rxsc_config(attrs, tb_rxsc))
  1705. return -EINVAL;
  1706. if (parse_sa_config(attrs, tb_sa))
  1707. return -EINVAL;
  1708. if (!validate_upd_sa(tb_sa))
  1709. return -EINVAL;
  1710. rtnl_lock();
  1711. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1712. &dev, &secy, &rx_sc, &assoc_num);
  1713. if (IS_ERR(rx_sa)) {
  1714. rtnl_unlock();
  1715. return PTR_ERR(rx_sa);
  1716. }
  1717. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1718. spin_lock_bh(&rx_sa->lock);
  1719. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1720. spin_unlock_bh(&rx_sa->lock);
  1721. }
  1722. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1723. rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1724. rtnl_unlock();
  1725. return 0;
  1726. }
  1727. static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
  1728. {
  1729. struct nlattr **attrs = info->attrs;
  1730. struct net_device *dev;
  1731. struct macsec_secy *secy;
  1732. struct macsec_rx_sc *rx_sc;
  1733. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1734. if (!attrs[MACSEC_ATTR_IFINDEX])
  1735. return -EINVAL;
  1736. if (parse_rxsc_config(attrs, tb_rxsc))
  1737. return -EINVAL;
  1738. if (!validate_add_rxsc(tb_rxsc))
  1739. return -EINVAL;
  1740. rtnl_lock();
  1741. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1742. if (IS_ERR(rx_sc)) {
  1743. rtnl_unlock();
  1744. return PTR_ERR(rx_sc);
  1745. }
  1746. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
  1747. bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1748. if (rx_sc->active != new)
  1749. secy->n_rx_sc += new ? 1 : -1;
  1750. rx_sc->active = new;
  1751. }
  1752. rtnl_unlock();
  1753. return 0;
  1754. }
  1755. static int copy_tx_sa_stats(struct sk_buff *skb,
  1756. struct macsec_tx_sa_stats __percpu *pstats)
  1757. {
  1758. struct macsec_tx_sa_stats sum = {0, };
  1759. int cpu;
  1760. for_each_possible_cpu(cpu) {
  1761. const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1762. sum.OutPktsProtected += stats->OutPktsProtected;
  1763. sum.OutPktsEncrypted += stats->OutPktsEncrypted;
  1764. }
  1765. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
  1766. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
  1767. return -EMSGSIZE;
  1768. return 0;
  1769. }
  1770. static int copy_rx_sa_stats(struct sk_buff *skb,
  1771. struct macsec_rx_sa_stats __percpu *pstats)
  1772. {
  1773. struct macsec_rx_sa_stats sum = {0, };
  1774. int cpu;
  1775. for_each_possible_cpu(cpu) {
  1776. const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1777. sum.InPktsOK += stats->InPktsOK;
  1778. sum.InPktsInvalid += stats->InPktsInvalid;
  1779. sum.InPktsNotValid += stats->InPktsNotValid;
  1780. sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
  1781. sum.InPktsUnusedSA += stats->InPktsUnusedSA;
  1782. }
  1783. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
  1784. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
  1785. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
  1786. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
  1787. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
  1788. return -EMSGSIZE;
  1789. return 0;
  1790. }
  1791. static int copy_rx_sc_stats(struct sk_buff *skb,
  1792. struct pcpu_rx_sc_stats __percpu *pstats)
  1793. {
  1794. struct macsec_rx_sc_stats sum = {0, };
  1795. int cpu;
  1796. for_each_possible_cpu(cpu) {
  1797. const struct pcpu_rx_sc_stats *stats;
  1798. struct macsec_rx_sc_stats tmp;
  1799. unsigned int start;
  1800. stats = per_cpu_ptr(pstats, cpu);
  1801. do {
  1802. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1803. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1804. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1805. sum.InOctetsValidated += tmp.InOctetsValidated;
  1806. sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
  1807. sum.InPktsUnchecked += tmp.InPktsUnchecked;
  1808. sum.InPktsDelayed += tmp.InPktsDelayed;
  1809. sum.InPktsOK += tmp.InPktsOK;
  1810. sum.InPktsInvalid += tmp.InPktsInvalid;
  1811. sum.InPktsLate += tmp.InPktsLate;
  1812. sum.InPktsNotValid += tmp.InPktsNotValid;
  1813. sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
  1814. sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
  1815. }
  1816. if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
  1817. sum.InOctetsValidated,
  1818. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1819. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
  1820. sum.InOctetsDecrypted,
  1821. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1822. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
  1823. sum.InPktsUnchecked,
  1824. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1825. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
  1826. sum.InPktsDelayed,
  1827. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1828. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
  1829. sum.InPktsOK,
  1830. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1831. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
  1832. sum.InPktsInvalid,
  1833. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1834. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
  1835. sum.InPktsLate,
  1836. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1837. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
  1838. sum.InPktsNotValid,
  1839. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1840. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
  1841. sum.InPktsNotUsingSA,
  1842. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1843. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
  1844. sum.InPktsUnusedSA,
  1845. MACSEC_RXSC_STATS_ATTR_PAD))
  1846. return -EMSGSIZE;
  1847. return 0;
  1848. }
  1849. static int copy_tx_sc_stats(struct sk_buff *skb,
  1850. struct pcpu_tx_sc_stats __percpu *pstats)
  1851. {
  1852. struct macsec_tx_sc_stats sum = {0, };
  1853. int cpu;
  1854. for_each_possible_cpu(cpu) {
  1855. const struct pcpu_tx_sc_stats *stats;
  1856. struct macsec_tx_sc_stats tmp;
  1857. unsigned int start;
  1858. stats = per_cpu_ptr(pstats, cpu);
  1859. do {
  1860. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1861. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1862. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1863. sum.OutPktsProtected += tmp.OutPktsProtected;
  1864. sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
  1865. sum.OutOctetsProtected += tmp.OutOctetsProtected;
  1866. sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
  1867. }
  1868. if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
  1869. sum.OutPktsProtected,
  1870. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1871. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
  1872. sum.OutPktsEncrypted,
  1873. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1874. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
  1875. sum.OutOctetsProtected,
  1876. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1877. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
  1878. sum.OutOctetsEncrypted,
  1879. MACSEC_TXSC_STATS_ATTR_PAD))
  1880. return -EMSGSIZE;
  1881. return 0;
  1882. }
  1883. static int copy_secy_stats(struct sk_buff *skb,
  1884. struct pcpu_secy_stats __percpu *pstats)
  1885. {
  1886. struct macsec_dev_stats sum = {0, };
  1887. int cpu;
  1888. for_each_possible_cpu(cpu) {
  1889. const struct pcpu_secy_stats *stats;
  1890. struct macsec_dev_stats tmp;
  1891. unsigned int start;
  1892. stats = per_cpu_ptr(pstats, cpu);
  1893. do {
  1894. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1895. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1896. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1897. sum.OutPktsUntagged += tmp.OutPktsUntagged;
  1898. sum.InPktsUntagged += tmp.InPktsUntagged;
  1899. sum.OutPktsTooLong += tmp.OutPktsTooLong;
  1900. sum.InPktsNoTag += tmp.InPktsNoTag;
  1901. sum.InPktsBadTag += tmp.InPktsBadTag;
  1902. sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
  1903. sum.InPktsNoSCI += tmp.InPktsNoSCI;
  1904. sum.InPktsOverrun += tmp.InPktsOverrun;
  1905. }
  1906. if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
  1907. sum.OutPktsUntagged,
  1908. MACSEC_SECY_STATS_ATTR_PAD) ||
  1909. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
  1910. sum.InPktsUntagged,
  1911. MACSEC_SECY_STATS_ATTR_PAD) ||
  1912. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
  1913. sum.OutPktsTooLong,
  1914. MACSEC_SECY_STATS_ATTR_PAD) ||
  1915. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
  1916. sum.InPktsNoTag,
  1917. MACSEC_SECY_STATS_ATTR_PAD) ||
  1918. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
  1919. sum.InPktsBadTag,
  1920. MACSEC_SECY_STATS_ATTR_PAD) ||
  1921. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
  1922. sum.InPktsUnknownSCI,
  1923. MACSEC_SECY_STATS_ATTR_PAD) ||
  1924. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
  1925. sum.InPktsNoSCI,
  1926. MACSEC_SECY_STATS_ATTR_PAD) ||
  1927. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
  1928. sum.InPktsOverrun,
  1929. MACSEC_SECY_STATS_ATTR_PAD))
  1930. return -EMSGSIZE;
  1931. return 0;
  1932. }
  1933. static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
  1934. {
  1935. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1936. struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
  1937. u64 csid;
  1938. if (!secy_nest)
  1939. return 1;
  1940. switch (secy->key_len) {
  1941. case MACSEC_GCM_AES_128_SAK_LEN:
  1942. csid = MACSEC_DEFAULT_CIPHER_ID;
  1943. break;
  1944. case MACSEC_GCM_AES_256_SAK_LEN:
  1945. csid = MACSEC_CIPHER_ID_GCM_AES_256;
  1946. break;
  1947. default:
  1948. goto cancel;
  1949. }
  1950. if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
  1951. MACSEC_SECY_ATTR_PAD) ||
  1952. nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
  1953. csid, MACSEC_SECY_ATTR_PAD) ||
  1954. nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
  1955. nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
  1956. nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
  1957. nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
  1958. nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
  1959. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
  1960. nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
  1961. nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
  1962. nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
  1963. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
  1964. goto cancel;
  1965. if (secy->replay_protect) {
  1966. if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
  1967. goto cancel;
  1968. }
  1969. nla_nest_end(skb, secy_nest);
  1970. return 0;
  1971. cancel:
  1972. nla_nest_cancel(skb, secy_nest);
  1973. return 1;
  1974. }
  1975. static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
  1976. struct sk_buff *skb, struct netlink_callback *cb)
  1977. {
  1978. struct macsec_rx_sc *rx_sc;
  1979. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1980. struct nlattr *txsa_list, *rxsc_list;
  1981. int i, j;
  1982. void *hdr;
  1983. struct nlattr *attr;
  1984. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1985. &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
  1986. if (!hdr)
  1987. return -EMSGSIZE;
  1988. genl_dump_check_consistent(cb, hdr);
  1989. if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
  1990. goto nla_put_failure;
  1991. if (nla_put_secy(secy, skb))
  1992. goto nla_put_failure;
  1993. attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
  1994. if (!attr)
  1995. goto nla_put_failure;
  1996. if (copy_tx_sc_stats(skb, tx_sc->stats)) {
  1997. nla_nest_cancel(skb, attr);
  1998. goto nla_put_failure;
  1999. }
  2000. nla_nest_end(skb, attr);
  2001. attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
  2002. if (!attr)
  2003. goto nla_put_failure;
  2004. if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
  2005. nla_nest_cancel(skb, attr);
  2006. goto nla_put_failure;
  2007. }
  2008. nla_nest_end(skb, attr);
  2009. txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
  2010. if (!txsa_list)
  2011. goto nla_put_failure;
  2012. for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
  2013. struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
  2014. struct nlattr *txsa_nest;
  2015. if (!tx_sa)
  2016. continue;
  2017. txsa_nest = nla_nest_start(skb, j++);
  2018. if (!txsa_nest) {
  2019. nla_nest_cancel(skb, txsa_list);
  2020. goto nla_put_failure;
  2021. }
  2022. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2023. nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
  2024. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
  2025. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
  2026. nla_nest_cancel(skb, txsa_nest);
  2027. nla_nest_cancel(skb, txsa_list);
  2028. goto nla_put_failure;
  2029. }
  2030. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  2031. if (!attr) {
  2032. nla_nest_cancel(skb, txsa_nest);
  2033. nla_nest_cancel(skb, txsa_list);
  2034. goto nla_put_failure;
  2035. }
  2036. if (copy_tx_sa_stats(skb, tx_sa->stats)) {
  2037. nla_nest_cancel(skb, attr);
  2038. nla_nest_cancel(skb, txsa_nest);
  2039. nla_nest_cancel(skb, txsa_list);
  2040. goto nla_put_failure;
  2041. }
  2042. nla_nest_end(skb, attr);
  2043. nla_nest_end(skb, txsa_nest);
  2044. }
  2045. nla_nest_end(skb, txsa_list);
  2046. rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
  2047. if (!rxsc_list)
  2048. goto nla_put_failure;
  2049. j = 1;
  2050. for_each_rxsc_rtnl(secy, rx_sc) {
  2051. int k;
  2052. struct nlattr *rxsa_list;
  2053. struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
  2054. if (!rxsc_nest) {
  2055. nla_nest_cancel(skb, rxsc_list);
  2056. goto nla_put_failure;
  2057. }
  2058. if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
  2059. nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
  2060. MACSEC_RXSC_ATTR_PAD)) {
  2061. nla_nest_cancel(skb, rxsc_nest);
  2062. nla_nest_cancel(skb, rxsc_list);
  2063. goto nla_put_failure;
  2064. }
  2065. attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
  2066. if (!attr) {
  2067. nla_nest_cancel(skb, rxsc_nest);
  2068. nla_nest_cancel(skb, rxsc_list);
  2069. goto nla_put_failure;
  2070. }
  2071. if (copy_rx_sc_stats(skb, rx_sc->stats)) {
  2072. nla_nest_cancel(skb, attr);
  2073. nla_nest_cancel(skb, rxsc_nest);
  2074. nla_nest_cancel(skb, rxsc_list);
  2075. goto nla_put_failure;
  2076. }
  2077. nla_nest_end(skb, attr);
  2078. rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
  2079. if (!rxsa_list) {
  2080. nla_nest_cancel(skb, rxsc_nest);
  2081. nla_nest_cancel(skb, rxsc_list);
  2082. goto nla_put_failure;
  2083. }
  2084. for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
  2085. struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
  2086. struct nlattr *rxsa_nest;
  2087. if (!rx_sa)
  2088. continue;
  2089. rxsa_nest = nla_nest_start(skb, k++);
  2090. if (!rxsa_nest) {
  2091. nla_nest_cancel(skb, rxsa_list);
  2092. nla_nest_cancel(skb, rxsc_nest);
  2093. nla_nest_cancel(skb, rxsc_list);
  2094. goto nla_put_failure;
  2095. }
  2096. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  2097. if (!attr) {
  2098. nla_nest_cancel(skb, rxsa_list);
  2099. nla_nest_cancel(skb, rxsc_nest);
  2100. nla_nest_cancel(skb, rxsc_list);
  2101. goto nla_put_failure;
  2102. }
  2103. if (copy_rx_sa_stats(skb, rx_sa->stats)) {
  2104. nla_nest_cancel(skb, attr);
  2105. nla_nest_cancel(skb, rxsa_list);
  2106. nla_nest_cancel(skb, rxsc_nest);
  2107. nla_nest_cancel(skb, rxsc_list);
  2108. goto nla_put_failure;
  2109. }
  2110. nla_nest_end(skb, attr);
  2111. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2112. nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
  2113. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
  2114. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
  2115. nla_nest_cancel(skb, rxsa_nest);
  2116. nla_nest_cancel(skb, rxsc_nest);
  2117. nla_nest_cancel(skb, rxsc_list);
  2118. goto nla_put_failure;
  2119. }
  2120. nla_nest_end(skb, rxsa_nest);
  2121. }
  2122. nla_nest_end(skb, rxsa_list);
  2123. nla_nest_end(skb, rxsc_nest);
  2124. }
  2125. nla_nest_end(skb, rxsc_list);
  2126. genlmsg_end(skb, hdr);
  2127. return 0;
  2128. nla_put_failure:
  2129. genlmsg_cancel(skb, hdr);
  2130. return -EMSGSIZE;
  2131. }
  2132. static int macsec_generation = 1; /* protected by RTNL */
  2133. static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
  2134. {
  2135. struct net *net = sock_net(skb->sk);
  2136. struct net_device *dev;
  2137. int dev_idx, d;
  2138. dev_idx = cb->args[0];
  2139. d = 0;
  2140. rtnl_lock();
  2141. cb->seq = macsec_generation;
  2142. for_each_netdev(net, dev) {
  2143. struct macsec_secy *secy;
  2144. if (d < dev_idx)
  2145. goto next;
  2146. if (!netif_is_macsec(dev))
  2147. goto next;
  2148. secy = &macsec_priv(dev)->secy;
  2149. if (dump_secy(secy, dev, skb, cb) < 0)
  2150. goto done;
  2151. next:
  2152. d++;
  2153. }
  2154. done:
  2155. rtnl_unlock();
  2156. cb->args[0] = d;
  2157. return skb->len;
  2158. }
  2159. static const struct genl_ops macsec_genl_ops[] = {
  2160. {
  2161. .cmd = MACSEC_CMD_GET_TXSC,
  2162. .dumpit = macsec_dump_txsc,
  2163. .policy = macsec_genl_policy,
  2164. },
  2165. {
  2166. .cmd = MACSEC_CMD_ADD_RXSC,
  2167. .doit = macsec_add_rxsc,
  2168. .policy = macsec_genl_policy,
  2169. .flags = GENL_ADMIN_PERM,
  2170. },
  2171. {
  2172. .cmd = MACSEC_CMD_DEL_RXSC,
  2173. .doit = macsec_del_rxsc,
  2174. .policy = macsec_genl_policy,
  2175. .flags = GENL_ADMIN_PERM,
  2176. },
  2177. {
  2178. .cmd = MACSEC_CMD_UPD_RXSC,
  2179. .doit = macsec_upd_rxsc,
  2180. .policy = macsec_genl_policy,
  2181. .flags = GENL_ADMIN_PERM,
  2182. },
  2183. {
  2184. .cmd = MACSEC_CMD_ADD_TXSA,
  2185. .doit = macsec_add_txsa,
  2186. .policy = macsec_genl_policy,
  2187. .flags = GENL_ADMIN_PERM,
  2188. },
  2189. {
  2190. .cmd = MACSEC_CMD_DEL_TXSA,
  2191. .doit = macsec_del_txsa,
  2192. .policy = macsec_genl_policy,
  2193. .flags = GENL_ADMIN_PERM,
  2194. },
  2195. {
  2196. .cmd = MACSEC_CMD_UPD_TXSA,
  2197. .doit = macsec_upd_txsa,
  2198. .policy = macsec_genl_policy,
  2199. .flags = GENL_ADMIN_PERM,
  2200. },
  2201. {
  2202. .cmd = MACSEC_CMD_ADD_RXSA,
  2203. .doit = macsec_add_rxsa,
  2204. .policy = macsec_genl_policy,
  2205. .flags = GENL_ADMIN_PERM,
  2206. },
  2207. {
  2208. .cmd = MACSEC_CMD_DEL_RXSA,
  2209. .doit = macsec_del_rxsa,
  2210. .policy = macsec_genl_policy,
  2211. .flags = GENL_ADMIN_PERM,
  2212. },
  2213. {
  2214. .cmd = MACSEC_CMD_UPD_RXSA,
  2215. .doit = macsec_upd_rxsa,
  2216. .policy = macsec_genl_policy,
  2217. .flags = GENL_ADMIN_PERM,
  2218. },
  2219. };
  2220. static struct genl_family macsec_fam __ro_after_init = {
  2221. .name = MACSEC_GENL_NAME,
  2222. .hdrsize = 0,
  2223. .version = MACSEC_GENL_VERSION,
  2224. .maxattr = MACSEC_ATTR_MAX,
  2225. .netnsok = true,
  2226. .module = THIS_MODULE,
  2227. .ops = macsec_genl_ops,
  2228. .n_ops = ARRAY_SIZE(macsec_genl_ops),
  2229. };
  2230. static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
  2231. struct net_device *dev)
  2232. {
  2233. struct macsec_dev *macsec = netdev_priv(dev);
  2234. struct macsec_secy *secy = &macsec->secy;
  2235. struct pcpu_secy_stats *secy_stats;
  2236. int ret, len;
  2237. /* 10.5 */
  2238. if (!secy->protect_frames) {
  2239. secy_stats = this_cpu_ptr(macsec->stats);
  2240. u64_stats_update_begin(&secy_stats->syncp);
  2241. secy_stats->stats.OutPktsUntagged++;
  2242. u64_stats_update_end(&secy_stats->syncp);
  2243. skb->dev = macsec->real_dev;
  2244. len = skb->len;
  2245. ret = dev_queue_xmit(skb);
  2246. count_tx(dev, ret, len);
  2247. return ret;
  2248. }
  2249. if (!secy->operational) {
  2250. kfree_skb(skb);
  2251. dev->stats.tx_dropped++;
  2252. return NETDEV_TX_OK;
  2253. }
  2254. skb = macsec_encrypt(skb, dev);
  2255. if (IS_ERR(skb)) {
  2256. if (PTR_ERR(skb) != -EINPROGRESS)
  2257. dev->stats.tx_dropped++;
  2258. return NETDEV_TX_OK;
  2259. }
  2260. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  2261. macsec_encrypt_finish(skb, dev);
  2262. len = skb->len;
  2263. ret = dev_queue_xmit(skb);
  2264. count_tx(dev, ret, len);
  2265. return ret;
  2266. }
  2267. #define MACSEC_FEATURES \
  2268. (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
  2269. static struct lock_class_key macsec_netdev_addr_lock_key;
  2270. static int macsec_dev_init(struct net_device *dev)
  2271. {
  2272. struct macsec_dev *macsec = macsec_priv(dev);
  2273. struct net_device *real_dev = macsec->real_dev;
  2274. int err;
  2275. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  2276. if (!dev->tstats)
  2277. return -ENOMEM;
  2278. err = gro_cells_init(&macsec->gro_cells, dev);
  2279. if (err) {
  2280. free_percpu(dev->tstats);
  2281. return err;
  2282. }
  2283. dev->features = real_dev->features & MACSEC_FEATURES;
  2284. dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
  2285. dev->needed_headroom = real_dev->needed_headroom +
  2286. MACSEC_NEEDED_HEADROOM;
  2287. dev->needed_tailroom = real_dev->needed_tailroom +
  2288. MACSEC_NEEDED_TAILROOM;
  2289. if (is_zero_ether_addr(dev->dev_addr))
  2290. eth_hw_addr_inherit(dev, real_dev);
  2291. if (is_zero_ether_addr(dev->broadcast))
  2292. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  2293. return 0;
  2294. }
  2295. static void macsec_dev_uninit(struct net_device *dev)
  2296. {
  2297. struct macsec_dev *macsec = macsec_priv(dev);
  2298. gro_cells_destroy(&macsec->gro_cells);
  2299. free_percpu(dev->tstats);
  2300. }
  2301. static netdev_features_t macsec_fix_features(struct net_device *dev,
  2302. netdev_features_t features)
  2303. {
  2304. struct macsec_dev *macsec = macsec_priv(dev);
  2305. struct net_device *real_dev = macsec->real_dev;
  2306. features &= (real_dev->features & MACSEC_FEATURES) |
  2307. NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
  2308. features |= NETIF_F_LLTX;
  2309. return features;
  2310. }
  2311. static int macsec_dev_open(struct net_device *dev)
  2312. {
  2313. struct macsec_dev *macsec = macsec_priv(dev);
  2314. struct net_device *real_dev = macsec->real_dev;
  2315. int err;
  2316. if (!(real_dev->flags & IFF_UP))
  2317. return -ENETDOWN;
  2318. err = dev_uc_add(real_dev, dev->dev_addr);
  2319. if (err < 0)
  2320. return err;
  2321. if (dev->flags & IFF_ALLMULTI) {
  2322. err = dev_set_allmulti(real_dev, 1);
  2323. if (err < 0)
  2324. goto del_unicast;
  2325. }
  2326. if (dev->flags & IFF_PROMISC) {
  2327. err = dev_set_promiscuity(real_dev, 1);
  2328. if (err < 0)
  2329. goto clear_allmulti;
  2330. }
  2331. if (netif_carrier_ok(real_dev))
  2332. netif_carrier_on(dev);
  2333. return 0;
  2334. clear_allmulti:
  2335. if (dev->flags & IFF_ALLMULTI)
  2336. dev_set_allmulti(real_dev, -1);
  2337. del_unicast:
  2338. dev_uc_del(real_dev, dev->dev_addr);
  2339. netif_carrier_off(dev);
  2340. return err;
  2341. }
  2342. static int macsec_dev_stop(struct net_device *dev)
  2343. {
  2344. struct macsec_dev *macsec = macsec_priv(dev);
  2345. struct net_device *real_dev = macsec->real_dev;
  2346. netif_carrier_off(dev);
  2347. dev_mc_unsync(real_dev, dev);
  2348. dev_uc_unsync(real_dev, dev);
  2349. if (dev->flags & IFF_ALLMULTI)
  2350. dev_set_allmulti(real_dev, -1);
  2351. if (dev->flags & IFF_PROMISC)
  2352. dev_set_promiscuity(real_dev, -1);
  2353. dev_uc_del(real_dev, dev->dev_addr);
  2354. return 0;
  2355. }
  2356. static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
  2357. {
  2358. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2359. if (!(dev->flags & IFF_UP))
  2360. return;
  2361. if (change & IFF_ALLMULTI)
  2362. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  2363. if (change & IFF_PROMISC)
  2364. dev_set_promiscuity(real_dev,
  2365. dev->flags & IFF_PROMISC ? 1 : -1);
  2366. }
  2367. static void macsec_dev_set_rx_mode(struct net_device *dev)
  2368. {
  2369. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2370. dev_mc_sync(real_dev, dev);
  2371. dev_uc_sync(real_dev, dev);
  2372. }
  2373. static int macsec_set_mac_address(struct net_device *dev, void *p)
  2374. {
  2375. struct macsec_dev *macsec = macsec_priv(dev);
  2376. struct net_device *real_dev = macsec->real_dev;
  2377. struct sockaddr *addr = p;
  2378. int err;
  2379. if (!is_valid_ether_addr(addr->sa_data))
  2380. return -EADDRNOTAVAIL;
  2381. if (!(dev->flags & IFF_UP))
  2382. goto out;
  2383. err = dev_uc_add(real_dev, addr->sa_data);
  2384. if (err < 0)
  2385. return err;
  2386. dev_uc_del(real_dev, dev->dev_addr);
  2387. out:
  2388. ether_addr_copy(dev->dev_addr, addr->sa_data);
  2389. return 0;
  2390. }
  2391. static int macsec_change_mtu(struct net_device *dev, int new_mtu)
  2392. {
  2393. struct macsec_dev *macsec = macsec_priv(dev);
  2394. unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
  2395. if (macsec->real_dev->mtu - extra < new_mtu)
  2396. return -ERANGE;
  2397. dev->mtu = new_mtu;
  2398. return 0;
  2399. }
  2400. static void macsec_get_stats64(struct net_device *dev,
  2401. struct rtnl_link_stats64 *s)
  2402. {
  2403. int cpu;
  2404. if (!dev->tstats)
  2405. return;
  2406. for_each_possible_cpu(cpu) {
  2407. struct pcpu_sw_netstats *stats;
  2408. struct pcpu_sw_netstats tmp;
  2409. int start;
  2410. stats = per_cpu_ptr(dev->tstats, cpu);
  2411. do {
  2412. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2413. tmp.rx_packets = stats->rx_packets;
  2414. tmp.rx_bytes = stats->rx_bytes;
  2415. tmp.tx_packets = stats->tx_packets;
  2416. tmp.tx_bytes = stats->tx_bytes;
  2417. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2418. s->rx_packets += tmp.rx_packets;
  2419. s->rx_bytes += tmp.rx_bytes;
  2420. s->tx_packets += tmp.tx_packets;
  2421. s->tx_bytes += tmp.tx_bytes;
  2422. }
  2423. s->rx_dropped = dev->stats.rx_dropped;
  2424. s->tx_dropped = dev->stats.tx_dropped;
  2425. }
  2426. static int macsec_get_iflink(const struct net_device *dev)
  2427. {
  2428. return macsec_priv(dev)->real_dev->ifindex;
  2429. }
  2430. static int macsec_get_nest_level(struct net_device *dev)
  2431. {
  2432. return macsec_priv(dev)->nest_level;
  2433. }
  2434. static const struct net_device_ops macsec_netdev_ops = {
  2435. .ndo_init = macsec_dev_init,
  2436. .ndo_uninit = macsec_dev_uninit,
  2437. .ndo_open = macsec_dev_open,
  2438. .ndo_stop = macsec_dev_stop,
  2439. .ndo_fix_features = macsec_fix_features,
  2440. .ndo_change_mtu = macsec_change_mtu,
  2441. .ndo_set_rx_mode = macsec_dev_set_rx_mode,
  2442. .ndo_change_rx_flags = macsec_dev_change_rx_flags,
  2443. .ndo_set_mac_address = macsec_set_mac_address,
  2444. .ndo_start_xmit = macsec_start_xmit,
  2445. .ndo_get_stats64 = macsec_get_stats64,
  2446. .ndo_get_iflink = macsec_get_iflink,
  2447. .ndo_get_lock_subclass = macsec_get_nest_level,
  2448. };
  2449. static const struct device_type macsec_type = {
  2450. .name = "macsec",
  2451. };
  2452. static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
  2453. [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
  2454. [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
  2455. [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
  2456. [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
  2457. [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
  2458. [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
  2459. [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
  2460. [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
  2461. [IFLA_MACSEC_ES] = { .type = NLA_U8 },
  2462. [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
  2463. [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
  2464. [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
  2465. };
  2466. static void macsec_free_netdev(struct net_device *dev)
  2467. {
  2468. struct macsec_dev *macsec = macsec_priv(dev);
  2469. struct net_device *real_dev = macsec->real_dev;
  2470. free_percpu(macsec->stats);
  2471. free_percpu(macsec->secy.tx_sc.stats);
  2472. dev_put(real_dev);
  2473. }
  2474. static void macsec_setup(struct net_device *dev)
  2475. {
  2476. ether_setup(dev);
  2477. dev->min_mtu = 0;
  2478. dev->max_mtu = ETH_MAX_MTU;
  2479. dev->priv_flags |= IFF_NO_QUEUE;
  2480. dev->netdev_ops = &macsec_netdev_ops;
  2481. dev->needs_free_netdev = true;
  2482. dev->priv_destructor = macsec_free_netdev;
  2483. SET_NETDEV_DEVTYPE(dev, &macsec_type);
  2484. eth_zero_addr(dev->broadcast);
  2485. }
  2486. static int macsec_changelink_common(struct net_device *dev,
  2487. struct nlattr *data[])
  2488. {
  2489. struct macsec_secy *secy;
  2490. struct macsec_tx_sc *tx_sc;
  2491. secy = &macsec_priv(dev)->secy;
  2492. tx_sc = &secy->tx_sc;
  2493. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2494. struct macsec_tx_sa *tx_sa;
  2495. tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
  2496. tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
  2497. secy->operational = tx_sa && tx_sa->active;
  2498. }
  2499. if (data[IFLA_MACSEC_WINDOW])
  2500. secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
  2501. if (data[IFLA_MACSEC_ENCRYPT])
  2502. tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
  2503. if (data[IFLA_MACSEC_PROTECT])
  2504. secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
  2505. if (data[IFLA_MACSEC_INC_SCI])
  2506. tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
  2507. if (data[IFLA_MACSEC_ES])
  2508. tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
  2509. if (data[IFLA_MACSEC_SCB])
  2510. tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
  2511. if (data[IFLA_MACSEC_REPLAY_PROTECT])
  2512. secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
  2513. if (data[IFLA_MACSEC_VALIDATION])
  2514. secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
  2515. if (data[IFLA_MACSEC_CIPHER_SUITE]) {
  2516. switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
  2517. case MACSEC_CIPHER_ID_GCM_AES_128:
  2518. case MACSEC_DEFAULT_CIPHER_ID:
  2519. secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
  2520. break;
  2521. case MACSEC_CIPHER_ID_GCM_AES_256:
  2522. secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
  2523. break;
  2524. default:
  2525. return -EINVAL;
  2526. }
  2527. }
  2528. return 0;
  2529. }
  2530. static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
  2531. struct nlattr *data[],
  2532. struct netlink_ext_ack *extack)
  2533. {
  2534. if (!data)
  2535. return 0;
  2536. if (data[IFLA_MACSEC_CIPHER_SUITE] ||
  2537. data[IFLA_MACSEC_ICV_LEN] ||
  2538. data[IFLA_MACSEC_SCI] ||
  2539. data[IFLA_MACSEC_PORT])
  2540. return -EINVAL;
  2541. return macsec_changelink_common(dev, data);
  2542. }
  2543. static void macsec_del_dev(struct macsec_dev *macsec)
  2544. {
  2545. int i;
  2546. while (macsec->secy.rx_sc) {
  2547. struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
  2548. rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
  2549. free_rx_sc(rx_sc);
  2550. }
  2551. for (i = 0; i < MACSEC_NUM_AN; i++) {
  2552. struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
  2553. if (sa) {
  2554. RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
  2555. clear_tx_sa(sa);
  2556. }
  2557. }
  2558. }
  2559. static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
  2560. {
  2561. struct macsec_dev *macsec = macsec_priv(dev);
  2562. struct net_device *real_dev = macsec->real_dev;
  2563. unregister_netdevice_queue(dev, head);
  2564. list_del_rcu(&macsec->secys);
  2565. macsec_del_dev(macsec);
  2566. netdev_upper_dev_unlink(real_dev, dev);
  2567. macsec_generation++;
  2568. }
  2569. static void macsec_dellink(struct net_device *dev, struct list_head *head)
  2570. {
  2571. struct macsec_dev *macsec = macsec_priv(dev);
  2572. struct net_device *real_dev = macsec->real_dev;
  2573. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2574. macsec_common_dellink(dev, head);
  2575. if (list_empty(&rxd->secys)) {
  2576. netdev_rx_handler_unregister(real_dev);
  2577. kfree(rxd);
  2578. }
  2579. }
  2580. static int register_macsec_dev(struct net_device *real_dev,
  2581. struct net_device *dev)
  2582. {
  2583. struct macsec_dev *macsec = macsec_priv(dev);
  2584. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2585. if (!rxd) {
  2586. int err;
  2587. rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
  2588. if (!rxd)
  2589. return -ENOMEM;
  2590. INIT_LIST_HEAD(&rxd->secys);
  2591. err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
  2592. rxd);
  2593. if (err < 0) {
  2594. kfree(rxd);
  2595. return err;
  2596. }
  2597. }
  2598. list_add_tail_rcu(&macsec->secys, &rxd->secys);
  2599. return 0;
  2600. }
  2601. static bool sci_exists(struct net_device *dev, sci_t sci)
  2602. {
  2603. struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
  2604. struct macsec_dev *macsec;
  2605. list_for_each_entry(macsec, &rxd->secys, secys) {
  2606. if (macsec->secy.sci == sci)
  2607. return true;
  2608. }
  2609. return false;
  2610. }
  2611. static sci_t dev_to_sci(struct net_device *dev, __be16 port)
  2612. {
  2613. return make_sci(dev->dev_addr, port);
  2614. }
  2615. static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
  2616. {
  2617. struct macsec_dev *macsec = macsec_priv(dev);
  2618. struct macsec_secy *secy = &macsec->secy;
  2619. macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
  2620. if (!macsec->stats)
  2621. return -ENOMEM;
  2622. secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
  2623. if (!secy->tx_sc.stats) {
  2624. free_percpu(macsec->stats);
  2625. return -ENOMEM;
  2626. }
  2627. if (sci == MACSEC_UNDEF_SCI)
  2628. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2629. secy->netdev = dev;
  2630. secy->operational = true;
  2631. secy->key_len = DEFAULT_SAK_LEN;
  2632. secy->icv_len = icv_len;
  2633. secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
  2634. secy->protect_frames = true;
  2635. secy->replay_protect = false;
  2636. secy->sci = sci;
  2637. secy->tx_sc.active = true;
  2638. secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
  2639. secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
  2640. secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
  2641. secy->tx_sc.end_station = false;
  2642. secy->tx_sc.scb = false;
  2643. return 0;
  2644. }
  2645. static int macsec_newlink(struct net *net, struct net_device *dev,
  2646. struct nlattr *tb[], struct nlattr *data[],
  2647. struct netlink_ext_ack *extack)
  2648. {
  2649. struct macsec_dev *macsec = macsec_priv(dev);
  2650. struct net_device *real_dev;
  2651. int err;
  2652. sci_t sci;
  2653. u8 icv_len = DEFAULT_ICV_LEN;
  2654. rx_handler_func_t *rx_handler;
  2655. if (!tb[IFLA_LINK])
  2656. return -EINVAL;
  2657. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  2658. if (!real_dev)
  2659. return -ENODEV;
  2660. dev->priv_flags |= IFF_MACSEC;
  2661. macsec->real_dev = real_dev;
  2662. if (data && data[IFLA_MACSEC_ICV_LEN])
  2663. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2664. dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
  2665. rx_handler = rtnl_dereference(real_dev->rx_handler);
  2666. if (rx_handler && rx_handler != macsec_handle_frame)
  2667. return -EBUSY;
  2668. err = register_netdevice(dev);
  2669. if (err < 0)
  2670. return err;
  2671. dev_hold(real_dev);
  2672. macsec->nest_level = dev_get_nest_level(real_dev) + 1;
  2673. netdev_lockdep_set_classes(dev);
  2674. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  2675. &macsec_netdev_addr_lock_key,
  2676. macsec_get_nest_level(dev));
  2677. err = netdev_upper_dev_link(real_dev, dev, extack);
  2678. if (err < 0)
  2679. goto unregister;
  2680. /* need to be already registered so that ->init has run and
  2681. * the MAC addr is set
  2682. */
  2683. if (data && data[IFLA_MACSEC_SCI])
  2684. sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
  2685. else if (data && data[IFLA_MACSEC_PORT])
  2686. sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
  2687. else
  2688. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2689. if (rx_handler && sci_exists(real_dev, sci)) {
  2690. err = -EBUSY;
  2691. goto unlink;
  2692. }
  2693. err = macsec_add_dev(dev, sci, icv_len);
  2694. if (err)
  2695. goto unlink;
  2696. if (data) {
  2697. err = macsec_changelink_common(dev, data);
  2698. if (err)
  2699. goto del_dev;
  2700. }
  2701. err = register_macsec_dev(real_dev, dev);
  2702. if (err < 0)
  2703. goto del_dev;
  2704. macsec_generation++;
  2705. return 0;
  2706. del_dev:
  2707. macsec_del_dev(macsec);
  2708. unlink:
  2709. netdev_upper_dev_unlink(real_dev, dev);
  2710. unregister:
  2711. unregister_netdevice(dev);
  2712. return err;
  2713. }
  2714. static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
  2715. struct netlink_ext_ack *extack)
  2716. {
  2717. u64 csid = MACSEC_DEFAULT_CIPHER_ID;
  2718. u8 icv_len = DEFAULT_ICV_LEN;
  2719. int flag;
  2720. bool es, scb, sci;
  2721. if (!data)
  2722. return 0;
  2723. if (data[IFLA_MACSEC_CIPHER_SUITE])
  2724. csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
  2725. if (data[IFLA_MACSEC_ICV_LEN]) {
  2726. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2727. if (icv_len != DEFAULT_ICV_LEN) {
  2728. char dummy_key[DEFAULT_SAK_LEN] = { 0 };
  2729. struct crypto_aead *dummy_tfm;
  2730. dummy_tfm = macsec_alloc_tfm(dummy_key,
  2731. DEFAULT_SAK_LEN,
  2732. icv_len);
  2733. if (IS_ERR(dummy_tfm))
  2734. return PTR_ERR(dummy_tfm);
  2735. crypto_free_aead(dummy_tfm);
  2736. }
  2737. }
  2738. switch (csid) {
  2739. case MACSEC_CIPHER_ID_GCM_AES_128:
  2740. case MACSEC_CIPHER_ID_GCM_AES_256:
  2741. case MACSEC_DEFAULT_CIPHER_ID:
  2742. if (icv_len < MACSEC_MIN_ICV_LEN ||
  2743. icv_len > MACSEC_STD_ICV_LEN)
  2744. return -EINVAL;
  2745. break;
  2746. default:
  2747. return -EINVAL;
  2748. }
  2749. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2750. if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
  2751. return -EINVAL;
  2752. }
  2753. for (flag = IFLA_MACSEC_ENCODING_SA + 1;
  2754. flag < IFLA_MACSEC_VALIDATION;
  2755. flag++) {
  2756. if (data[flag]) {
  2757. if (nla_get_u8(data[flag]) > 1)
  2758. return -EINVAL;
  2759. }
  2760. }
  2761. es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
  2762. sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
  2763. scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
  2764. if ((sci && (scb || es)) || (scb && es))
  2765. return -EINVAL;
  2766. if (data[IFLA_MACSEC_VALIDATION] &&
  2767. nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
  2768. return -EINVAL;
  2769. if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
  2770. nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
  2771. !data[IFLA_MACSEC_WINDOW])
  2772. return -EINVAL;
  2773. return 0;
  2774. }
  2775. static struct net *macsec_get_link_net(const struct net_device *dev)
  2776. {
  2777. return dev_net(macsec_priv(dev)->real_dev);
  2778. }
  2779. static size_t macsec_get_size(const struct net_device *dev)
  2780. {
  2781. return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
  2782. nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
  2783. nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
  2784. nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
  2785. nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
  2786. nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
  2787. nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
  2788. nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
  2789. nla_total_size(1) + /* IFLA_MACSEC_ES */
  2790. nla_total_size(1) + /* IFLA_MACSEC_SCB */
  2791. nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
  2792. nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
  2793. 0;
  2794. }
  2795. static int macsec_fill_info(struct sk_buff *skb,
  2796. const struct net_device *dev)
  2797. {
  2798. struct macsec_secy *secy = &macsec_priv(dev)->secy;
  2799. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2800. u64 csid;
  2801. switch (secy->key_len) {
  2802. case MACSEC_GCM_AES_128_SAK_LEN:
  2803. csid = MACSEC_DEFAULT_CIPHER_ID;
  2804. break;
  2805. case MACSEC_GCM_AES_256_SAK_LEN:
  2806. csid = MACSEC_CIPHER_ID_GCM_AES_256;
  2807. break;
  2808. default:
  2809. goto nla_put_failure;
  2810. }
  2811. if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
  2812. IFLA_MACSEC_PAD) ||
  2813. nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
  2814. nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
  2815. csid, IFLA_MACSEC_PAD) ||
  2816. nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
  2817. nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
  2818. nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
  2819. nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
  2820. nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
  2821. nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
  2822. nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
  2823. nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
  2824. 0)
  2825. goto nla_put_failure;
  2826. if (secy->replay_protect) {
  2827. if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
  2828. goto nla_put_failure;
  2829. }
  2830. return 0;
  2831. nla_put_failure:
  2832. return -EMSGSIZE;
  2833. }
  2834. static struct rtnl_link_ops macsec_link_ops __read_mostly = {
  2835. .kind = "macsec",
  2836. .priv_size = sizeof(struct macsec_dev),
  2837. .maxtype = IFLA_MACSEC_MAX,
  2838. .policy = macsec_rtnl_policy,
  2839. .setup = macsec_setup,
  2840. .validate = macsec_validate_attr,
  2841. .newlink = macsec_newlink,
  2842. .changelink = macsec_changelink,
  2843. .dellink = macsec_dellink,
  2844. .get_size = macsec_get_size,
  2845. .fill_info = macsec_fill_info,
  2846. .get_link_net = macsec_get_link_net,
  2847. };
  2848. static bool is_macsec_master(struct net_device *dev)
  2849. {
  2850. return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
  2851. }
  2852. static int macsec_notify(struct notifier_block *this, unsigned long event,
  2853. void *ptr)
  2854. {
  2855. struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
  2856. LIST_HEAD(head);
  2857. if (!is_macsec_master(real_dev))
  2858. return NOTIFY_DONE;
  2859. switch (event) {
  2860. case NETDEV_UNREGISTER: {
  2861. struct macsec_dev *m, *n;
  2862. struct macsec_rxh_data *rxd;
  2863. rxd = macsec_data_rtnl(real_dev);
  2864. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  2865. macsec_common_dellink(m->secy.netdev, &head);
  2866. }
  2867. netdev_rx_handler_unregister(real_dev);
  2868. kfree(rxd);
  2869. unregister_netdevice_many(&head);
  2870. break;
  2871. }
  2872. case NETDEV_CHANGEMTU: {
  2873. struct macsec_dev *m;
  2874. struct macsec_rxh_data *rxd;
  2875. rxd = macsec_data_rtnl(real_dev);
  2876. list_for_each_entry(m, &rxd->secys, secys) {
  2877. struct net_device *dev = m->secy.netdev;
  2878. unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
  2879. macsec_extra_len(true));
  2880. if (dev->mtu > mtu)
  2881. dev_set_mtu(dev, mtu);
  2882. }
  2883. }
  2884. }
  2885. return NOTIFY_OK;
  2886. }
  2887. static struct notifier_block macsec_notifier = {
  2888. .notifier_call = macsec_notify,
  2889. };
  2890. static int __init macsec_init(void)
  2891. {
  2892. int err;
  2893. pr_info("MACsec IEEE 802.1AE\n");
  2894. err = register_netdevice_notifier(&macsec_notifier);
  2895. if (err)
  2896. return err;
  2897. err = rtnl_link_register(&macsec_link_ops);
  2898. if (err)
  2899. goto notifier;
  2900. err = genl_register_family(&macsec_fam);
  2901. if (err)
  2902. goto rtnl;
  2903. return 0;
  2904. rtnl:
  2905. rtnl_link_unregister(&macsec_link_ops);
  2906. notifier:
  2907. unregister_netdevice_notifier(&macsec_notifier);
  2908. return err;
  2909. }
  2910. static void __exit macsec_exit(void)
  2911. {
  2912. genl_unregister_family(&macsec_fam);
  2913. rtnl_link_unregister(&macsec_link_ops);
  2914. unregister_netdevice_notifier(&macsec_notifier);
  2915. rcu_barrier();
  2916. }
  2917. module_init(macsec_init);
  2918. module_exit(macsec_exit);
  2919. MODULE_ALIAS_RTNL_LINK("macsec");
  2920. MODULE_ALIAS_GENL_FAMILY("macsec");
  2921. MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
  2922. MODULE_LICENSE("GPL v2");