macsec.c 84 KB

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