macsec.c 84 KB

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