af_mpls.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/skbuff.h>
  4. #include <linux/socket.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/net.h>
  7. #include <linux/module.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/ipv6.h>
  10. #include <linux/mpls.h>
  11. #include <linux/netconf.h>
  12. #include <linux/nospec.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/percpu.h>
  15. #include <net/gso.h>
  16. #include <net/ip.h>
  17. #include <net/dst.h>
  18. #include <net/sock.h>
  19. #include <net/arp.h>
  20. #include <net/ip_fib.h>
  21. #include <net/netevent.h>
  22. #include <net/ip_tunnels.h>
  23. #include <net/netns/generic.h>
  24. #if IS_ENABLED(CONFIG_IPV6)
  25. #include <net/ipv6.h>
  26. #endif
  27. #include <net/ipv6_stubs.h>
  28. #include <net/rtnh.h>
  29. #include "internal.h"
  30. /* max memory we will use for mpls_route */
  31. #define MAX_MPLS_ROUTE_MEM 4096
  32. /* Maximum number of labels to look ahead at when selecting a path of
  33. * a multipath route
  34. */
  35. #define MAX_MP_SELECT_LABELS 4
  36. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  37. static int label_limit = (1 << 20) - 1;
  38. static int ttl_max = 255;
  39. #if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
  40. static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
  41. {
  42. return sizeof(struct mpls_shim_hdr);
  43. }
  44. static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
  45. .encap_hlen = ipgre_mpls_encap_hlen,
  46. };
  47. static int ipgre_tunnel_encap_add_mpls_ops(void)
  48. {
  49. return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  50. }
  51. static void ipgre_tunnel_encap_del_mpls_ops(void)
  52. {
  53. ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  54. }
  55. #else
  56. static int ipgre_tunnel_encap_add_mpls_ops(void)
  57. {
  58. return 0;
  59. }
  60. static void ipgre_tunnel_encap_del_mpls_ops(void)
  61. {
  62. }
  63. #endif
  64. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  65. struct nlmsghdr *nlh, struct net *net, u32 portid,
  66. unsigned int nlm_flags);
  67. static struct mpls_route *mpls_route_input(struct net *net, unsigned int index)
  68. {
  69. struct mpls_route __rcu **platform_label;
  70. platform_label = mpls_dereference(net, net->mpls.platform_label);
  71. return mpls_dereference(net, platform_label[index]);
  72. }
  73. static struct mpls_route __rcu **mpls_platform_label_rcu(struct net *net, size_t *platform_labels)
  74. {
  75. struct mpls_route __rcu **platform_label;
  76. unsigned int sequence;
  77. do {
  78. sequence = read_seqcount_begin(&net->mpls.platform_label_seq);
  79. platform_label = rcu_dereference(net->mpls.platform_label);
  80. *platform_labels = net->mpls.platform_labels;
  81. } while (read_seqcount_retry(&net->mpls.platform_label_seq, sequence));
  82. return platform_label;
  83. }
  84. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned int index)
  85. {
  86. struct mpls_route __rcu **platform_label;
  87. size_t platform_labels;
  88. platform_label = mpls_platform_label_rcu(net, &platform_labels);
  89. if (index >= platform_labels)
  90. return NULL;
  91. return rcu_dereference(platform_label[index]);
  92. }
  93. bool mpls_output_possible(const struct net_device *dev)
  94. {
  95. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  96. }
  97. EXPORT_SYMBOL_GPL(mpls_output_possible);
  98. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  99. {
  100. return (u8 *)nh + rt->rt_via_offset;
  101. }
  102. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  103. const struct mpls_nh *nh)
  104. {
  105. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  106. }
  107. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  108. {
  109. /* The size of the layer 2.5 labels to be added for this route */
  110. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  111. }
  112. unsigned int mpls_dev_mtu(const struct net_device *dev)
  113. {
  114. /* The amount of data the layer 2 frame can hold */
  115. return dev->mtu;
  116. }
  117. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  118. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  119. {
  120. if (skb->len <= mtu)
  121. return false;
  122. if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
  123. return false;
  124. return true;
  125. }
  126. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  127. void mpls_stats_inc_outucastpkts(struct net *net,
  128. struct net_device *dev,
  129. const struct sk_buff *skb)
  130. {
  131. struct mpls_dev *mdev;
  132. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  133. mdev = mpls_dev_rcu(dev);
  134. if (mdev)
  135. MPLS_INC_STATS_LEN(mdev, skb->len,
  136. tx_packets,
  137. tx_bytes);
  138. } else if (skb->protocol == htons(ETH_P_IP)) {
  139. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  140. #if IS_ENABLED(CONFIG_IPV6)
  141. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  142. struct inet6_dev *in6dev = in6_dev_rcu(dev);
  143. if (in6dev)
  144. IP6_UPD_PO_STATS(net, in6dev,
  145. IPSTATS_MIB_OUT, skb->len);
  146. #endif
  147. }
  148. }
  149. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  150. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  151. {
  152. struct mpls_entry_decoded dec;
  153. unsigned int mpls_hdr_len = 0;
  154. struct mpls_shim_hdr *hdr;
  155. bool eli_seen = false;
  156. int label_index;
  157. u32 hash = 0;
  158. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  159. label_index++) {
  160. mpls_hdr_len += sizeof(*hdr);
  161. if (!pskb_may_pull(skb, mpls_hdr_len))
  162. break;
  163. /* Read and decode the current label */
  164. hdr = mpls_hdr(skb) + label_index;
  165. dec = mpls_entry_decode(hdr);
  166. /* RFC6790 - reserved labels MUST NOT be used as keys
  167. * for the load-balancing function
  168. */
  169. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  170. hash = jhash_1word(dec.label, hash);
  171. /* The entropy label follows the entropy label
  172. * indicator, so this means that the entropy
  173. * label was just added to the hash - no need to
  174. * go any deeper either in the label stack or in the
  175. * payload
  176. */
  177. if (eli_seen)
  178. break;
  179. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  180. eli_seen = true;
  181. }
  182. if (!dec.bos)
  183. continue;
  184. /* found bottom label; does skb have room for a header? */
  185. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  186. const struct iphdr *v4hdr;
  187. v4hdr = (const struct iphdr *)(hdr + 1);
  188. if (v4hdr->version == 4) {
  189. hash = jhash_3words(ntohl(v4hdr->saddr),
  190. ntohl(v4hdr->daddr),
  191. v4hdr->protocol, hash);
  192. } else if (v4hdr->version == 6 &&
  193. pskb_may_pull(skb, mpls_hdr_len +
  194. sizeof(struct ipv6hdr))) {
  195. const struct ipv6hdr *v6hdr;
  196. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  197. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  198. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  199. hash = jhash_1word(v6hdr->nexthdr, hash);
  200. }
  201. }
  202. break;
  203. }
  204. return hash;
  205. }
  206. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  207. {
  208. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  209. }
  210. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  211. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  212. * Since those fields can change at any moment, use READ_ONCE to
  213. * access both.
  214. */
  215. static const struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  216. struct sk_buff *skb)
  217. {
  218. u32 hash = 0;
  219. int nh_index = 0;
  220. int n = 0;
  221. u8 alive;
  222. /* No need to look further into packet if there's only
  223. * one path
  224. */
  225. if (rt->rt_nhn == 1)
  226. return rt->rt_nh;
  227. alive = READ_ONCE(rt->rt_nhn_alive);
  228. if (alive == 0)
  229. return NULL;
  230. hash = mpls_multipath_hash(rt, skb);
  231. nh_index = hash % alive;
  232. if (alive == rt->rt_nhn)
  233. goto out;
  234. for_nexthops(rt) {
  235. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  236. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  237. continue;
  238. if (n == nh_index)
  239. return nh;
  240. n++;
  241. } endfor_nexthops(rt);
  242. out:
  243. return mpls_get_nexthop(rt, nh_index);
  244. }
  245. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  246. struct sk_buff *skb, struct mpls_entry_decoded dec)
  247. {
  248. enum mpls_payload_type payload_type;
  249. bool success = false;
  250. /* The IPv4 code below accesses through the IPv4 header
  251. * checksum, which is 12 bytes into the packet.
  252. * The IPv6 code below accesses through the IPv6 hop limit
  253. * which is 8 bytes into the packet.
  254. *
  255. * For all supported cases there should always be at least 12
  256. * bytes of packet data present. The IPv4 header is 20 bytes
  257. * without options and the IPv6 header is always 40 bytes
  258. * long.
  259. */
  260. if (!pskb_may_pull(skb, 12))
  261. return false;
  262. payload_type = rt->rt_payload_type;
  263. if (payload_type == MPT_UNSPEC)
  264. payload_type = ip_hdr(skb)->version;
  265. switch (payload_type) {
  266. case MPT_IPV4: {
  267. struct iphdr *hdr4 = ip_hdr(skb);
  268. u8 new_ttl;
  269. skb->protocol = htons(ETH_P_IP);
  270. /* If propagating TTL, take the decremented TTL from
  271. * the incoming MPLS header, otherwise decrement the
  272. * TTL, but only if not 0 to avoid underflow.
  273. */
  274. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  275. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  276. net->mpls.ip_ttl_propagate))
  277. new_ttl = dec.ttl;
  278. else
  279. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  280. csum_replace2(&hdr4->check,
  281. htons(hdr4->ttl << 8),
  282. htons(new_ttl << 8));
  283. hdr4->ttl = new_ttl;
  284. success = true;
  285. break;
  286. }
  287. case MPT_IPV6: {
  288. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  289. skb->protocol = htons(ETH_P_IPV6);
  290. /* If propagating TTL, take the decremented TTL from
  291. * the incoming MPLS header, otherwise decrement the
  292. * hop limit, but only if not 0 to avoid underflow.
  293. */
  294. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  295. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  296. net->mpls.ip_ttl_propagate))
  297. hdr6->hop_limit = dec.ttl;
  298. else if (hdr6->hop_limit)
  299. hdr6->hop_limit = hdr6->hop_limit - 1;
  300. success = true;
  301. break;
  302. }
  303. case MPT_UNSPEC:
  304. /* Should have decided which protocol it is by now */
  305. break;
  306. }
  307. return success;
  308. }
  309. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  310. struct packet_type *pt, struct net_device *orig_dev)
  311. {
  312. struct net *net = dev_net_rcu(dev);
  313. struct mpls_shim_hdr *hdr;
  314. const struct mpls_nh *nh;
  315. struct mpls_route *rt;
  316. struct mpls_entry_decoded dec;
  317. struct net_device *out_dev;
  318. struct mpls_dev *out_mdev;
  319. struct mpls_dev *mdev;
  320. unsigned int hh_len;
  321. unsigned int new_header_size;
  322. unsigned int mtu;
  323. int err;
  324. /* Careful this entire function runs inside of an rcu critical section */
  325. mdev = mpls_dev_rcu(dev);
  326. if (!mdev)
  327. goto drop;
  328. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  329. rx_bytes);
  330. if (!mdev->input_enabled) {
  331. MPLS_INC_STATS(mdev, rx_dropped);
  332. goto drop;
  333. }
  334. if (skb->pkt_type != PACKET_HOST)
  335. goto err;
  336. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  337. goto err;
  338. if (!pskb_may_pull(skb, sizeof(*hdr)))
  339. goto err;
  340. skb_dst_drop(skb);
  341. /* Read and decode the label */
  342. hdr = mpls_hdr(skb);
  343. dec = mpls_entry_decode(hdr);
  344. rt = mpls_route_input_rcu(net, dec.label);
  345. if (!rt) {
  346. MPLS_INC_STATS(mdev, rx_noroute);
  347. goto drop;
  348. }
  349. nh = mpls_select_multipath(rt, skb);
  350. if (!nh)
  351. goto err;
  352. /* Pop the label */
  353. skb_pull(skb, sizeof(*hdr));
  354. skb_reset_network_header(skb);
  355. skb_orphan(skb);
  356. if (skb_warn_if_lro(skb))
  357. goto err;
  358. skb_forward_csum(skb);
  359. /* Verify ttl is valid */
  360. if (dec.ttl <= 1)
  361. goto err;
  362. /* Find the output device */
  363. out_dev = nh->nh_dev;
  364. if (!mpls_output_possible(out_dev))
  365. goto tx_err;
  366. /* Verify the destination can hold the packet */
  367. new_header_size = mpls_nh_header_size(nh);
  368. mtu = mpls_dev_mtu(out_dev);
  369. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  370. goto tx_err;
  371. hh_len = LL_RESERVED_SPACE(out_dev);
  372. if (!out_dev->header_ops)
  373. hh_len = 0;
  374. /* Ensure there is enough space for the headers in the skb */
  375. if (skb_cow(skb, hh_len + new_header_size))
  376. goto tx_err;
  377. skb->dev = out_dev;
  378. skb->protocol = htons(ETH_P_MPLS_UC);
  379. dec.ttl -= 1;
  380. if (unlikely(!new_header_size && dec.bos)) {
  381. /* Penultimate hop popping */
  382. if (!mpls_egress(net, rt, skb, dec))
  383. goto err;
  384. } else {
  385. bool bos;
  386. int i;
  387. skb_push(skb, new_header_size);
  388. skb_reset_network_header(skb);
  389. /* Push the new labels */
  390. hdr = mpls_hdr(skb);
  391. bos = dec.bos;
  392. for (i = nh->nh_labels - 1; i >= 0; i--) {
  393. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  394. dec.ttl, 0, bos);
  395. bos = false;
  396. }
  397. }
  398. mpls_stats_inc_outucastpkts(net, out_dev, skb);
  399. /* If via wasn't specified then send out using device address */
  400. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  401. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  402. out_dev->dev_addr, skb);
  403. else
  404. err = neigh_xmit(nh->nh_via_table, out_dev,
  405. mpls_nh_via(rt, nh), skb);
  406. if (err)
  407. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  408. __func__, err);
  409. return 0;
  410. tx_err:
  411. out_mdev = out_dev ? mpls_dev_rcu(out_dev) : NULL;
  412. if (out_mdev)
  413. MPLS_INC_STATS(out_mdev, tx_errors);
  414. goto drop;
  415. err:
  416. MPLS_INC_STATS(mdev, rx_errors);
  417. drop:
  418. kfree_skb(skb);
  419. return NET_RX_DROP;
  420. }
  421. static struct packet_type mpls_packet_type __read_mostly = {
  422. .type = cpu_to_be16(ETH_P_MPLS_UC),
  423. .func = mpls_forward,
  424. };
  425. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  426. [RTA_DST] = { .type = NLA_U32 },
  427. [RTA_OIF] = { .type = NLA_U32 },
  428. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  429. };
  430. struct mpls_route_config {
  431. u32 rc_protocol;
  432. u32 rc_ifindex;
  433. u8 rc_via_table;
  434. u8 rc_via_alen;
  435. u8 rc_via[MAX_VIA_ALEN];
  436. u32 rc_label;
  437. u8 rc_ttl_propagate;
  438. u8 rc_output_labels;
  439. u32 rc_output_label[MAX_NEW_LABELS];
  440. u32 rc_nlflags;
  441. enum mpls_payload_type rc_payload_type;
  442. struct nl_info rc_nlinfo;
  443. struct rtnexthop *rc_mp;
  444. int rc_mp_len;
  445. };
  446. /* all nexthops within a route have the same size based on max
  447. * number of labels and max via length for a hop
  448. */
  449. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  450. {
  451. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  452. struct mpls_route *rt;
  453. size_t size;
  454. size = sizeof(*rt) + num_nh * nh_size;
  455. if (size > MAX_MPLS_ROUTE_MEM)
  456. return ERR_PTR(-EINVAL);
  457. rt = kzalloc(size, GFP_KERNEL);
  458. if (!rt)
  459. return ERR_PTR(-ENOMEM);
  460. rt->rt_nhn = num_nh;
  461. rt->rt_nhn_alive = num_nh;
  462. rt->rt_nh_size = nh_size;
  463. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  464. return rt;
  465. }
  466. static void mpls_rt_free_rcu(struct rcu_head *head)
  467. {
  468. struct mpls_route *rt;
  469. rt = container_of(head, struct mpls_route, rt_rcu);
  470. change_nexthops(rt) {
  471. netdev_put(nh->nh_dev, &nh->nh_dev_tracker);
  472. } endfor_nexthops(rt);
  473. kfree(rt);
  474. }
  475. static void mpls_rt_free(struct mpls_route *rt)
  476. {
  477. if (rt)
  478. call_rcu(&rt->rt_rcu, mpls_rt_free_rcu);
  479. }
  480. static void mpls_notify_route(struct net *net, unsigned index,
  481. struct mpls_route *old, struct mpls_route *new,
  482. const struct nl_info *info)
  483. {
  484. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  485. unsigned portid = info ? info->portid : 0;
  486. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  487. struct mpls_route *rt = new ? new : old;
  488. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  489. /* Ignore reserved labels for now */
  490. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  491. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  492. }
  493. static void mpls_route_update(struct net *net, unsigned index,
  494. struct mpls_route *new,
  495. const struct nl_info *info)
  496. {
  497. struct mpls_route __rcu **platform_label;
  498. struct mpls_route *rt;
  499. platform_label = mpls_dereference(net, net->mpls.platform_label);
  500. rt = mpls_dereference(net, platform_label[index]);
  501. rcu_assign_pointer(platform_label[index], new);
  502. mpls_notify_route(net, index, rt, new, info);
  503. /* If we removed a route free it now */
  504. mpls_rt_free(rt);
  505. }
  506. static unsigned int find_free_label(struct net *net)
  507. {
  508. unsigned int index;
  509. for (index = MPLS_LABEL_FIRST_UNRESERVED;
  510. index < net->mpls.platform_labels;
  511. index++) {
  512. if (!mpls_route_input(net, index))
  513. return index;
  514. }
  515. return LABEL_NOT_SPECIFIED;
  516. }
  517. #if IS_ENABLED(CONFIG_INET)
  518. static struct net_device *inet_fib_lookup_dev(struct net *net,
  519. struct mpls_nh *nh,
  520. const void *addr)
  521. {
  522. struct net_device *dev;
  523. struct rtable *rt;
  524. struct in_addr daddr;
  525. memcpy(&daddr, addr, sizeof(struct in_addr));
  526. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0, RT_SCOPE_UNIVERSE);
  527. if (IS_ERR(rt))
  528. return ERR_CAST(rt);
  529. dev = rt->dst.dev;
  530. netdev_hold(dev, &nh->nh_dev_tracker, GFP_KERNEL);
  531. ip_rt_put(rt);
  532. return dev;
  533. }
  534. #else
  535. static struct net_device *inet_fib_lookup_dev(struct net *net,
  536. struct mpls_nh *nh,
  537. const void *addr)
  538. {
  539. return ERR_PTR(-EAFNOSUPPORT);
  540. }
  541. #endif
  542. #if IS_ENABLED(CONFIG_IPV6)
  543. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  544. struct mpls_nh *nh,
  545. const void *addr)
  546. {
  547. struct net_device *dev;
  548. struct dst_entry *dst;
  549. struct flowi6 fl6;
  550. if (!ipv6_stub)
  551. return ERR_PTR(-EAFNOSUPPORT);
  552. memset(&fl6, 0, sizeof(fl6));
  553. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  554. dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
  555. if (IS_ERR(dst))
  556. return ERR_CAST(dst);
  557. dev = dst->dev;
  558. netdev_hold(dev, &nh->nh_dev_tracker, GFP_KERNEL);
  559. dst_release(dst);
  560. return dev;
  561. }
  562. #else
  563. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  564. struct mpls_nh *nh,
  565. const void *addr)
  566. {
  567. return ERR_PTR(-EAFNOSUPPORT);
  568. }
  569. #endif
  570. static struct net_device *find_outdev(struct net *net,
  571. struct mpls_route *rt,
  572. struct mpls_nh *nh, int oif)
  573. {
  574. struct net_device *dev = NULL;
  575. if (!oif) {
  576. switch (nh->nh_via_table) {
  577. case NEIGH_ARP_TABLE:
  578. dev = inet_fib_lookup_dev(net, nh, mpls_nh_via(rt, nh));
  579. break;
  580. case NEIGH_ND_TABLE:
  581. dev = inet6_fib_lookup_dev(net, nh, mpls_nh_via(rt, nh));
  582. break;
  583. case NEIGH_LINK_TABLE:
  584. break;
  585. }
  586. } else {
  587. dev = netdev_get_by_index(net, oif,
  588. &nh->nh_dev_tracker, GFP_KERNEL);
  589. }
  590. if (!dev)
  591. return ERR_PTR(-ENODEV);
  592. if (IS_ERR(dev))
  593. return dev;
  594. nh->nh_dev = dev;
  595. return dev;
  596. }
  597. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  598. struct mpls_nh *nh, int oif)
  599. {
  600. struct net_device *dev = NULL;
  601. int err = -ENODEV;
  602. dev = find_outdev(net, rt, nh, oif);
  603. if (IS_ERR(dev)) {
  604. err = PTR_ERR(dev);
  605. goto errout;
  606. }
  607. /* Ensure this is a supported device */
  608. err = -EINVAL;
  609. if (!mpls_dev_get(net, dev))
  610. goto errout_put;
  611. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  612. (dev->addr_len != nh->nh_via_alen))
  613. goto errout_put;
  614. if (!(dev->flags & IFF_UP)) {
  615. nh->nh_flags |= RTNH_F_DEAD;
  616. } else {
  617. unsigned int flags;
  618. flags = netif_get_flags(dev);
  619. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  620. nh->nh_flags |= RTNH_F_LINKDOWN;
  621. }
  622. return 0;
  623. errout_put:
  624. netdev_put(nh->nh_dev, &nh->nh_dev_tracker);
  625. nh->nh_dev = NULL;
  626. errout:
  627. return err;
  628. }
  629. static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
  630. u8 via_addr[], struct netlink_ext_ack *extack)
  631. {
  632. struct rtvia *via = nla_data(nla);
  633. int err = -EINVAL;
  634. int alen;
  635. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
  636. NL_SET_ERR_MSG_ATTR(extack, nla,
  637. "Invalid attribute length for RTA_VIA");
  638. goto errout;
  639. }
  640. alen = nla_len(nla) -
  641. offsetof(struct rtvia, rtvia_addr);
  642. if (alen > MAX_VIA_ALEN) {
  643. NL_SET_ERR_MSG_ATTR(extack, nla,
  644. "Invalid address length for RTA_VIA");
  645. goto errout;
  646. }
  647. /* Validate the address family */
  648. switch (via->rtvia_family) {
  649. case AF_PACKET:
  650. *via_table = NEIGH_LINK_TABLE;
  651. break;
  652. case AF_INET:
  653. *via_table = NEIGH_ARP_TABLE;
  654. if (alen != 4)
  655. goto errout;
  656. break;
  657. case AF_INET6:
  658. *via_table = NEIGH_ND_TABLE;
  659. if (alen != 16)
  660. goto errout;
  661. break;
  662. default:
  663. /* Unsupported address family */
  664. goto errout;
  665. }
  666. memcpy(via_addr, via->rtvia_addr, alen);
  667. *via_alen = alen;
  668. err = 0;
  669. errout:
  670. return err;
  671. }
  672. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  673. struct mpls_route *rt)
  674. {
  675. struct net *net = cfg->rc_nlinfo.nl_net;
  676. struct mpls_nh *nh = rt->rt_nh;
  677. int err;
  678. int i;
  679. if (!nh)
  680. return -ENOMEM;
  681. nh->nh_labels = cfg->rc_output_labels;
  682. for (i = 0; i < nh->nh_labels; i++)
  683. nh->nh_label[i] = cfg->rc_output_label[i];
  684. nh->nh_via_table = cfg->rc_via_table;
  685. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  686. nh->nh_via_alen = cfg->rc_via_alen;
  687. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  688. if (err)
  689. goto errout;
  690. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  691. rt->rt_nhn_alive--;
  692. return 0;
  693. errout:
  694. return err;
  695. }
  696. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  697. struct mpls_nh *nh, int oif, struct nlattr *via,
  698. struct nlattr *newdst, u8 max_labels,
  699. struct netlink_ext_ack *extack)
  700. {
  701. int err = -ENOMEM;
  702. if (!nh)
  703. goto errout;
  704. if (newdst) {
  705. err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
  706. nh->nh_label, extack);
  707. if (err)
  708. goto errout;
  709. }
  710. if (via) {
  711. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  712. __mpls_nh_via(rt, nh), extack);
  713. if (err)
  714. goto errout;
  715. } else {
  716. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  717. }
  718. err = mpls_nh_assign_dev(net, rt, nh, oif);
  719. if (err)
  720. goto errout;
  721. return 0;
  722. errout:
  723. return err;
  724. }
  725. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  726. u8 cfg_via_alen, u8 *max_via_alen,
  727. u8 *max_labels)
  728. {
  729. int remaining = len;
  730. u8 nhs = 0;
  731. *max_via_alen = 0;
  732. *max_labels = 0;
  733. while (rtnh_ok(rtnh, remaining)) {
  734. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  735. int attrlen;
  736. u8 n_labels = 0;
  737. attrlen = rtnh_attrlen(rtnh);
  738. nla = nla_find(attrs, attrlen, RTA_VIA);
  739. if (nla && nla_len(nla) >=
  740. offsetof(struct rtvia, rtvia_addr)) {
  741. int via_alen = nla_len(nla) -
  742. offsetof(struct rtvia, rtvia_addr);
  743. if (via_alen <= MAX_VIA_ALEN)
  744. *max_via_alen = max_t(u16, *max_via_alen,
  745. via_alen);
  746. }
  747. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  748. if (nla &&
  749. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
  750. NULL, NULL) != 0)
  751. return 0;
  752. *max_labels = max_t(u8, *max_labels, n_labels);
  753. /* number of nexthops is tracked by a u8.
  754. * Check for overflow.
  755. */
  756. if (nhs == 255)
  757. return 0;
  758. nhs++;
  759. rtnh = rtnh_next(rtnh, &remaining);
  760. }
  761. /* leftover implies invalid nexthop configuration, discard it */
  762. return remaining > 0 ? 0 : nhs;
  763. }
  764. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  765. struct mpls_route *rt, u8 max_labels,
  766. struct netlink_ext_ack *extack)
  767. {
  768. struct rtnexthop *rtnh = cfg->rc_mp;
  769. struct nlattr *nla_via, *nla_newdst;
  770. int remaining = cfg->rc_mp_len;
  771. int err = 0;
  772. rt->rt_nhn = 0;
  773. change_nexthops(rt) {
  774. int attrlen;
  775. nla_via = NULL;
  776. nla_newdst = NULL;
  777. err = -EINVAL;
  778. if (!rtnh_ok(rtnh, remaining))
  779. goto errout;
  780. /* neither weighted multipath nor any flags
  781. * are supported
  782. */
  783. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  784. goto errout;
  785. attrlen = rtnh_attrlen(rtnh);
  786. if (attrlen > 0) {
  787. struct nlattr *attrs = rtnh_attrs(rtnh);
  788. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  789. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  790. }
  791. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  792. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  793. max_labels, extack);
  794. if (err)
  795. goto errout;
  796. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  797. rt->rt_nhn_alive--;
  798. rtnh = rtnh_next(rtnh, &remaining);
  799. rt->rt_nhn++;
  800. } endfor_nexthops(rt);
  801. return 0;
  802. errout:
  803. return err;
  804. }
  805. static bool mpls_label_ok(struct net *net, unsigned int *index,
  806. struct netlink_ext_ack *extack)
  807. {
  808. /* Reserved labels may not be set */
  809. if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
  810. NL_SET_ERR_MSG(extack,
  811. "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
  812. return false;
  813. }
  814. /* The full 20 bit range may not be supported. */
  815. if (*index >= net->mpls.platform_labels) {
  816. NL_SET_ERR_MSG(extack,
  817. "Label >= configured maximum in platform_labels");
  818. return false;
  819. }
  820. *index = array_index_nospec(*index, net->mpls.platform_labels);
  821. return true;
  822. }
  823. static int mpls_route_add(struct mpls_route_config *cfg,
  824. struct netlink_ext_ack *extack)
  825. {
  826. struct net *net = cfg->rc_nlinfo.nl_net;
  827. struct mpls_route *rt, *old;
  828. int err = -EINVAL;
  829. u8 max_via_alen;
  830. unsigned index;
  831. u8 max_labels;
  832. u8 nhs;
  833. index = cfg->rc_label;
  834. /* If a label was not specified during insert pick one */
  835. if ((index == LABEL_NOT_SPECIFIED) &&
  836. (cfg->rc_nlflags & NLM_F_CREATE)) {
  837. index = find_free_label(net);
  838. }
  839. if (!mpls_label_ok(net, &index, extack))
  840. goto errout;
  841. /* Append makes no sense with mpls */
  842. err = -EOPNOTSUPP;
  843. if (cfg->rc_nlflags & NLM_F_APPEND) {
  844. NL_SET_ERR_MSG(extack, "MPLS does not support route append");
  845. goto errout;
  846. }
  847. err = -EEXIST;
  848. old = mpls_route_input(net, index);
  849. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  850. goto errout;
  851. err = -EEXIST;
  852. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  853. goto errout;
  854. err = -ENOENT;
  855. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  856. goto errout;
  857. err = -EINVAL;
  858. if (cfg->rc_mp) {
  859. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  860. cfg->rc_via_alen, &max_via_alen,
  861. &max_labels);
  862. } else {
  863. max_via_alen = cfg->rc_via_alen;
  864. max_labels = cfg->rc_output_labels;
  865. nhs = 1;
  866. }
  867. if (nhs == 0) {
  868. NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
  869. goto errout;
  870. }
  871. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  872. if (IS_ERR(rt)) {
  873. err = PTR_ERR(rt);
  874. goto errout;
  875. }
  876. rt->rt_protocol = cfg->rc_protocol;
  877. rt->rt_payload_type = cfg->rc_payload_type;
  878. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  879. if (cfg->rc_mp)
  880. err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
  881. else
  882. err = mpls_nh_build_from_cfg(cfg, rt);
  883. if (err)
  884. goto freert;
  885. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  886. return 0;
  887. freert:
  888. mpls_rt_free(rt);
  889. errout:
  890. return err;
  891. }
  892. static int mpls_route_del(struct mpls_route_config *cfg,
  893. struct netlink_ext_ack *extack)
  894. {
  895. struct net *net = cfg->rc_nlinfo.nl_net;
  896. unsigned index;
  897. int err = -EINVAL;
  898. index = cfg->rc_label;
  899. if (!mpls_label_ok(net, &index, extack))
  900. goto errout;
  901. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  902. err = 0;
  903. errout:
  904. return err;
  905. }
  906. static void mpls_get_stats(struct mpls_dev *mdev,
  907. struct mpls_link_stats *stats)
  908. {
  909. struct mpls_pcpu_stats *p;
  910. int i;
  911. memset(stats, 0, sizeof(*stats));
  912. for_each_possible_cpu(i) {
  913. struct mpls_link_stats local;
  914. unsigned int start;
  915. p = per_cpu_ptr(mdev->stats, i);
  916. do {
  917. start = u64_stats_fetch_begin(&p->syncp);
  918. local = p->stats;
  919. } while (u64_stats_fetch_retry(&p->syncp, start));
  920. stats->rx_packets += local.rx_packets;
  921. stats->rx_bytes += local.rx_bytes;
  922. stats->tx_packets += local.tx_packets;
  923. stats->tx_bytes += local.tx_bytes;
  924. stats->rx_errors += local.rx_errors;
  925. stats->tx_errors += local.tx_errors;
  926. stats->rx_dropped += local.rx_dropped;
  927. stats->tx_dropped += local.tx_dropped;
  928. stats->rx_noroute += local.rx_noroute;
  929. }
  930. }
  931. static int mpls_fill_stats_af(struct sk_buff *skb,
  932. const struct net_device *dev)
  933. {
  934. struct mpls_link_stats *stats;
  935. struct mpls_dev *mdev;
  936. struct nlattr *nla;
  937. mdev = mpls_dev_rcu(dev);
  938. if (!mdev)
  939. return -ENODATA;
  940. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  941. sizeof(struct mpls_link_stats),
  942. MPLS_STATS_UNSPEC);
  943. if (!nla)
  944. return -EMSGSIZE;
  945. stats = nla_data(nla);
  946. mpls_get_stats(mdev, stats);
  947. return 0;
  948. }
  949. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  950. {
  951. struct mpls_dev *mdev;
  952. mdev = mpls_dev_rcu(dev);
  953. if (!mdev)
  954. return 0;
  955. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  956. }
  957. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  958. u32 portid, u32 seq, int event,
  959. unsigned int flags, int type)
  960. {
  961. struct nlmsghdr *nlh;
  962. struct netconfmsg *ncm;
  963. bool all = false;
  964. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  965. flags);
  966. if (!nlh)
  967. return -EMSGSIZE;
  968. if (type == NETCONFA_ALL)
  969. all = true;
  970. ncm = nlmsg_data(nlh);
  971. ncm->ncm_family = AF_MPLS;
  972. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  973. goto nla_put_failure;
  974. if ((all || type == NETCONFA_INPUT) &&
  975. nla_put_s32(skb, NETCONFA_INPUT,
  976. READ_ONCE(mdev->input_enabled)) < 0)
  977. goto nla_put_failure;
  978. nlmsg_end(skb, nlh);
  979. return 0;
  980. nla_put_failure:
  981. nlmsg_cancel(skb, nlh);
  982. return -EMSGSIZE;
  983. }
  984. static int mpls_netconf_msgsize_devconf(int type)
  985. {
  986. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  987. + nla_total_size(4); /* NETCONFA_IFINDEX */
  988. bool all = false;
  989. if (type == NETCONFA_ALL)
  990. all = true;
  991. if (all || type == NETCONFA_INPUT)
  992. size += nla_total_size(4);
  993. return size;
  994. }
  995. static void mpls_netconf_notify_devconf(struct net *net, int event,
  996. int type, struct mpls_dev *mdev)
  997. {
  998. struct sk_buff *skb;
  999. int err = -ENOBUFS;
  1000. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  1001. if (!skb)
  1002. goto errout;
  1003. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  1004. if (err < 0) {
  1005. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  1006. WARN_ON(err == -EMSGSIZE);
  1007. kfree_skb(skb);
  1008. goto errout;
  1009. }
  1010. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  1011. return;
  1012. errout:
  1013. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  1014. }
  1015. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  1016. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  1017. };
  1018. static int mpls_netconf_valid_get_req(struct sk_buff *skb,
  1019. const struct nlmsghdr *nlh,
  1020. struct nlattr **tb,
  1021. struct netlink_ext_ack *extack)
  1022. {
  1023. int i, err;
  1024. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
  1025. NL_SET_ERR_MSG_MOD(extack,
  1026. "Invalid header for netconf get request");
  1027. return -EINVAL;
  1028. }
  1029. if (!netlink_strict_get_check(skb))
  1030. return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
  1031. tb, NETCONFA_MAX,
  1032. devconf_mpls_policy, extack);
  1033. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
  1034. tb, NETCONFA_MAX,
  1035. devconf_mpls_policy, extack);
  1036. if (err)
  1037. return err;
  1038. for (i = 0; i <= NETCONFA_MAX; i++) {
  1039. if (!tb[i])
  1040. continue;
  1041. switch (i) {
  1042. case NETCONFA_IFINDEX:
  1043. break;
  1044. default:
  1045. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
  1046. return -EINVAL;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  1052. struct nlmsghdr *nlh,
  1053. struct netlink_ext_ack *extack)
  1054. {
  1055. struct net *net = sock_net(in_skb->sk);
  1056. struct nlattr *tb[NETCONFA_MAX + 1];
  1057. struct net_device *dev;
  1058. struct mpls_dev *mdev;
  1059. struct sk_buff *skb;
  1060. int ifindex;
  1061. int err;
  1062. err = mpls_netconf_valid_get_req(in_skb, nlh, tb, extack);
  1063. if (err < 0)
  1064. goto errout;
  1065. if (!tb[NETCONFA_IFINDEX]) {
  1066. err = -EINVAL;
  1067. goto errout;
  1068. }
  1069. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  1070. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  1071. if (!skb) {
  1072. err = -ENOBUFS;
  1073. goto errout;
  1074. }
  1075. rcu_read_lock();
  1076. dev = dev_get_by_index_rcu(net, ifindex);
  1077. if (!dev) {
  1078. err = -EINVAL;
  1079. goto errout_unlock;
  1080. }
  1081. mdev = mpls_dev_rcu(dev);
  1082. if (!mdev) {
  1083. err = -EINVAL;
  1084. goto errout_unlock;
  1085. }
  1086. err = mpls_netconf_fill_devconf(skb, mdev,
  1087. NETLINK_CB(in_skb).portid,
  1088. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  1089. NETCONFA_ALL);
  1090. if (err < 0) {
  1091. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  1092. WARN_ON(err == -EMSGSIZE);
  1093. goto errout_unlock;
  1094. }
  1095. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  1096. rcu_read_unlock();
  1097. errout:
  1098. return err;
  1099. errout_unlock:
  1100. rcu_read_unlock();
  1101. kfree_skb(skb);
  1102. goto errout;
  1103. }
  1104. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  1105. struct netlink_callback *cb)
  1106. {
  1107. const struct nlmsghdr *nlh = cb->nlh;
  1108. struct net *net = sock_net(skb->sk);
  1109. struct {
  1110. unsigned long ifindex;
  1111. } *ctx = (void *)cb->ctx;
  1112. struct net_device *dev;
  1113. struct mpls_dev *mdev;
  1114. int err = 0;
  1115. if (cb->strict_check) {
  1116. struct netlink_ext_ack *extack = cb->extack;
  1117. struct netconfmsg *ncm;
  1118. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  1119. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
  1120. return -EINVAL;
  1121. }
  1122. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  1123. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
  1124. return -EINVAL;
  1125. }
  1126. }
  1127. rcu_read_lock();
  1128. for_each_netdev_dump(net, dev, ctx->ifindex) {
  1129. mdev = mpls_dev_rcu(dev);
  1130. if (!mdev)
  1131. continue;
  1132. err = mpls_netconf_fill_devconf(skb, mdev,
  1133. NETLINK_CB(cb->skb).portid,
  1134. nlh->nlmsg_seq,
  1135. RTM_NEWNETCONF,
  1136. NLM_F_MULTI,
  1137. NETCONFA_ALL);
  1138. if (err < 0)
  1139. break;
  1140. }
  1141. rcu_read_unlock();
  1142. return err;
  1143. }
  1144. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  1145. (&((struct mpls_dev *)0)->field)
  1146. static int mpls_conf_proc(const struct ctl_table *ctl, int write,
  1147. void *buffer, size_t *lenp, loff_t *ppos)
  1148. {
  1149. int oval = *(int *)ctl->data;
  1150. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1151. if (write) {
  1152. struct mpls_dev *mdev = ctl->extra1;
  1153. int i = (int *)ctl->data - (int *)mdev;
  1154. struct net *net = ctl->extra2;
  1155. int val = *(int *)ctl->data;
  1156. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1157. val != oval) {
  1158. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1159. NETCONFA_INPUT, mdev);
  1160. }
  1161. }
  1162. return ret;
  1163. }
  1164. static const struct ctl_table mpls_dev_table[] = {
  1165. {
  1166. .procname = "input",
  1167. .maxlen = sizeof(int),
  1168. .mode = 0644,
  1169. .proc_handler = mpls_conf_proc,
  1170. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1171. },
  1172. };
  1173. static int mpls_dev_sysctl_register(struct net_device *dev,
  1174. struct mpls_dev *mdev)
  1175. {
  1176. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1177. size_t table_size = ARRAY_SIZE(mpls_dev_table);
  1178. struct net *net = dev_net(dev);
  1179. struct ctl_table *table;
  1180. int i;
  1181. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1182. if (!table)
  1183. goto out;
  1184. /* Table data contains only offsets relative to the base of
  1185. * the mdev at this point, so make them absolute.
  1186. */
  1187. for (i = 0; i < table_size; i++) {
  1188. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1189. table[i].extra1 = mdev;
  1190. table[i].extra2 = net;
  1191. }
  1192. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1193. mdev->sysctl = register_net_sysctl_sz(net, path, table, table_size);
  1194. if (!mdev->sysctl)
  1195. goto free;
  1196. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1197. return 0;
  1198. free:
  1199. kfree(table);
  1200. out:
  1201. mdev->sysctl = NULL;
  1202. return -ENOBUFS;
  1203. }
  1204. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1205. struct mpls_dev *mdev)
  1206. {
  1207. struct net *net = dev_net(dev);
  1208. const struct ctl_table *table;
  1209. if (!mdev->sysctl)
  1210. return;
  1211. table = mdev->sysctl->ctl_table_arg;
  1212. unregister_net_sysctl_table(mdev->sysctl);
  1213. kfree(table);
  1214. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1215. }
  1216. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1217. {
  1218. struct mpls_dev *mdev;
  1219. int err = -ENOMEM;
  1220. int i;
  1221. mdev = kzalloc_obj(*mdev);
  1222. if (!mdev)
  1223. return ERR_PTR(err);
  1224. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1225. if (!mdev->stats)
  1226. goto free;
  1227. for_each_possible_cpu(i) {
  1228. struct mpls_pcpu_stats *mpls_stats;
  1229. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1230. u64_stats_init(&mpls_stats->syncp);
  1231. }
  1232. mdev->dev = dev;
  1233. err = mpls_dev_sysctl_register(dev, mdev);
  1234. if (err)
  1235. goto free;
  1236. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1237. return mdev;
  1238. free:
  1239. free_percpu(mdev->stats);
  1240. kfree(mdev);
  1241. return ERR_PTR(err);
  1242. }
  1243. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1244. {
  1245. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1246. free_percpu(mdev->stats);
  1247. kfree(mdev);
  1248. }
  1249. static int mpls_ifdown(struct net_device *dev, int event)
  1250. {
  1251. struct net *net = dev_net(dev);
  1252. unsigned int index;
  1253. for (index = 0; index < net->mpls.platform_labels; index++) {
  1254. struct mpls_route *rt;
  1255. bool nh_del = false;
  1256. u8 alive = 0;
  1257. rt = mpls_route_input(net, index);
  1258. if (!rt)
  1259. continue;
  1260. if (event == NETDEV_UNREGISTER) {
  1261. u8 deleted = 0;
  1262. for_nexthops(rt) {
  1263. if (!nh->nh_dev || nh->nh_dev == dev)
  1264. deleted++;
  1265. if (nh->nh_dev == dev)
  1266. nh_del = true;
  1267. } endfor_nexthops(rt);
  1268. /* if there are no more nexthops, delete the route */
  1269. if (deleted == rt->rt_nhn) {
  1270. mpls_route_update(net, index, NULL, NULL);
  1271. continue;
  1272. }
  1273. if (nh_del) {
  1274. size_t size = sizeof(*rt) + rt->rt_nhn *
  1275. rt->rt_nh_size;
  1276. struct mpls_route *orig = rt;
  1277. rt = kmemdup(orig, size, GFP_KERNEL);
  1278. if (!rt)
  1279. return -ENOMEM;
  1280. }
  1281. }
  1282. change_nexthops(rt) {
  1283. unsigned int nh_flags = nh->nh_flags;
  1284. if (nh->nh_dev != dev) {
  1285. if (nh_del)
  1286. netdev_hold(nh->nh_dev, &nh->nh_dev_tracker,
  1287. GFP_KERNEL);
  1288. goto next;
  1289. }
  1290. switch (event) {
  1291. case NETDEV_DOWN:
  1292. case NETDEV_UNREGISTER:
  1293. nh_flags |= RTNH_F_DEAD;
  1294. fallthrough;
  1295. case NETDEV_CHANGE:
  1296. nh_flags |= RTNH_F_LINKDOWN;
  1297. break;
  1298. }
  1299. if (event == NETDEV_UNREGISTER)
  1300. nh->nh_dev = NULL;
  1301. if (nh->nh_flags != nh_flags)
  1302. WRITE_ONCE(nh->nh_flags, nh_flags);
  1303. next:
  1304. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1305. alive++;
  1306. } endfor_nexthops(rt);
  1307. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1308. if (nh_del)
  1309. mpls_route_update(net, index, rt, NULL);
  1310. }
  1311. return 0;
  1312. }
  1313. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1314. {
  1315. struct net *net = dev_net(dev);
  1316. unsigned int index;
  1317. u8 alive;
  1318. for (index = 0; index < net->mpls.platform_labels; index++) {
  1319. struct mpls_route *rt;
  1320. rt = mpls_route_input(net, index);
  1321. if (!rt)
  1322. continue;
  1323. alive = 0;
  1324. change_nexthops(rt) {
  1325. unsigned int nh_flags = nh->nh_flags;
  1326. if (!(nh_flags & flags)) {
  1327. alive++;
  1328. continue;
  1329. }
  1330. if (nh->nh_dev != dev)
  1331. continue;
  1332. alive++;
  1333. nh_flags &= ~flags;
  1334. WRITE_ONCE(nh->nh_flags, nh_flags);
  1335. } endfor_nexthops(rt);
  1336. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1337. }
  1338. }
  1339. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1340. void *ptr)
  1341. {
  1342. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1343. struct net *net = dev_net(dev);
  1344. struct mpls_dev *mdev;
  1345. unsigned int flags;
  1346. int err;
  1347. mutex_lock(&net->mpls.platform_mutex);
  1348. if (event == NETDEV_REGISTER) {
  1349. mdev = mpls_add_dev(dev);
  1350. if (IS_ERR(mdev)) {
  1351. err = PTR_ERR(mdev);
  1352. goto err;
  1353. }
  1354. goto out;
  1355. }
  1356. mdev = mpls_dev_get(net, dev);
  1357. if (!mdev)
  1358. goto out;
  1359. switch (event) {
  1360. case NETDEV_DOWN:
  1361. err = mpls_ifdown(dev, event);
  1362. if (err)
  1363. goto err;
  1364. break;
  1365. case NETDEV_UP:
  1366. flags = netif_get_flags(dev);
  1367. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1368. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1369. else
  1370. mpls_ifup(dev, RTNH_F_DEAD);
  1371. break;
  1372. case NETDEV_CHANGE:
  1373. flags = netif_get_flags(dev);
  1374. if (flags & (IFF_RUNNING | IFF_LOWER_UP)) {
  1375. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1376. } else {
  1377. err = mpls_ifdown(dev, event);
  1378. if (err)
  1379. goto err;
  1380. }
  1381. break;
  1382. case NETDEV_UNREGISTER:
  1383. err = mpls_ifdown(dev, event);
  1384. if (err)
  1385. goto err;
  1386. mdev = mpls_dev_get(net, dev);
  1387. if (mdev) {
  1388. mpls_dev_sysctl_unregister(dev, mdev);
  1389. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1390. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1391. }
  1392. break;
  1393. case NETDEV_CHANGENAME:
  1394. mdev = mpls_dev_get(net, dev);
  1395. if (mdev) {
  1396. mpls_dev_sysctl_unregister(dev, mdev);
  1397. err = mpls_dev_sysctl_register(dev, mdev);
  1398. if (err)
  1399. goto err;
  1400. }
  1401. break;
  1402. }
  1403. out:
  1404. mutex_unlock(&net->mpls.platform_mutex);
  1405. return NOTIFY_OK;
  1406. err:
  1407. mutex_unlock(&net->mpls.platform_mutex);
  1408. return notifier_from_errno(err);
  1409. }
  1410. static struct notifier_block mpls_dev_notifier = {
  1411. .notifier_call = mpls_dev_notify,
  1412. };
  1413. static int nla_put_via(struct sk_buff *skb,
  1414. u8 table, const void *addr, int alen)
  1415. {
  1416. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1417. AF_INET, AF_INET6, AF_PACKET,
  1418. };
  1419. struct nlattr *nla;
  1420. struct rtvia *via;
  1421. int family = AF_UNSPEC;
  1422. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1423. if (!nla)
  1424. return -EMSGSIZE;
  1425. if (table <= NEIGH_NR_TABLES)
  1426. family = table_to_family[table];
  1427. via = nla_data(nla);
  1428. via->rtvia_family = family;
  1429. memcpy(via->rtvia_addr, addr, alen);
  1430. return 0;
  1431. }
  1432. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1433. u8 labels, const u32 label[])
  1434. {
  1435. struct nlattr *nla;
  1436. struct mpls_shim_hdr *nla_label;
  1437. bool bos;
  1438. int i;
  1439. nla = nla_reserve(skb, attrtype, labels*4);
  1440. if (!nla)
  1441. return -EMSGSIZE;
  1442. nla_label = nla_data(nla);
  1443. bos = true;
  1444. for (i = labels - 1; i >= 0; i--) {
  1445. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1446. bos = false;
  1447. }
  1448. return 0;
  1449. }
  1450. EXPORT_SYMBOL_GPL(nla_put_labels);
  1451. int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
  1452. u32 label[], struct netlink_ext_ack *extack)
  1453. {
  1454. unsigned len = nla_len(nla);
  1455. struct mpls_shim_hdr *nla_label;
  1456. u8 nla_labels;
  1457. bool bos;
  1458. int i;
  1459. /* len needs to be an even multiple of 4 (the label size). Number
  1460. * of labels is a u8 so check for overflow.
  1461. */
  1462. if (len & 3 || len / 4 > 255) {
  1463. NL_SET_ERR_MSG_ATTR(extack, nla,
  1464. "Invalid length for labels attribute");
  1465. return -EINVAL;
  1466. }
  1467. /* Limit the number of new labels allowed */
  1468. nla_labels = len/4;
  1469. if (nla_labels > max_labels) {
  1470. NL_SET_ERR_MSG(extack, "Too many labels");
  1471. return -EINVAL;
  1472. }
  1473. /* when label == NULL, caller wants number of labels */
  1474. if (!label)
  1475. goto out;
  1476. nla_label = nla_data(nla);
  1477. bos = true;
  1478. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1479. struct mpls_entry_decoded dec;
  1480. dec = mpls_entry_decode(nla_label + i);
  1481. /* Ensure the bottom of stack flag is properly set
  1482. * and ttl and tc are both clear.
  1483. */
  1484. if (dec.ttl) {
  1485. NL_SET_ERR_MSG_ATTR(extack, nla,
  1486. "TTL in label must be 0");
  1487. return -EINVAL;
  1488. }
  1489. if (dec.tc) {
  1490. NL_SET_ERR_MSG_ATTR(extack, nla,
  1491. "Traffic class in label must be 0");
  1492. return -EINVAL;
  1493. }
  1494. if (dec.bos != bos) {
  1495. NL_SET_BAD_ATTR(extack, nla);
  1496. if (bos) {
  1497. NL_SET_ERR_MSG(extack,
  1498. "BOS bit must be set in first label");
  1499. } else {
  1500. NL_SET_ERR_MSG(extack,
  1501. "BOS bit can only be set in first label");
  1502. }
  1503. return -EINVAL;
  1504. }
  1505. switch (dec.label) {
  1506. case MPLS_LABEL_IMPLNULL:
  1507. /* RFC3032: This is a label that an LSR may
  1508. * assign and distribute, but which never
  1509. * actually appears in the encapsulation.
  1510. */
  1511. NL_SET_ERR_MSG_ATTR(extack, nla,
  1512. "Implicit NULL Label (3) can not be used in encapsulation");
  1513. return -EINVAL;
  1514. }
  1515. label[i] = dec.label;
  1516. }
  1517. out:
  1518. *labels = nla_labels;
  1519. return 0;
  1520. }
  1521. EXPORT_SYMBOL_GPL(nla_get_labels);
  1522. static int rtm_to_route_config(struct sk_buff *skb,
  1523. struct nlmsghdr *nlh,
  1524. struct mpls_route_config *cfg,
  1525. struct netlink_ext_ack *extack)
  1526. {
  1527. struct rtmsg *rtm;
  1528. struct nlattr *tb[RTA_MAX+1];
  1529. int index;
  1530. int err;
  1531. err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  1532. rtm_mpls_policy, extack);
  1533. if (err < 0)
  1534. goto errout;
  1535. err = -EINVAL;
  1536. rtm = nlmsg_data(nlh);
  1537. if (rtm->rtm_family != AF_MPLS) {
  1538. NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
  1539. goto errout;
  1540. }
  1541. if (rtm->rtm_dst_len != 20) {
  1542. NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
  1543. goto errout;
  1544. }
  1545. if (rtm->rtm_src_len != 0) {
  1546. NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
  1547. goto errout;
  1548. }
  1549. if (rtm->rtm_tos != 0) {
  1550. NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
  1551. goto errout;
  1552. }
  1553. if (rtm->rtm_table != RT_TABLE_MAIN) {
  1554. NL_SET_ERR_MSG(extack,
  1555. "MPLS only supports the main route table");
  1556. goto errout;
  1557. }
  1558. /* Any value is acceptable for rtm_protocol */
  1559. /* As mpls uses destination specific addresses
  1560. * (or source specific address in the case of multicast)
  1561. * all addresses have universal scope.
  1562. */
  1563. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
  1564. NL_SET_ERR_MSG(extack,
  1565. "Invalid route scope - MPLS only supports UNIVERSE");
  1566. goto errout;
  1567. }
  1568. if (rtm->rtm_type != RTN_UNICAST) {
  1569. NL_SET_ERR_MSG(extack,
  1570. "Invalid route type - MPLS only supports UNICAST");
  1571. goto errout;
  1572. }
  1573. if (rtm->rtm_flags != 0) {
  1574. NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
  1575. goto errout;
  1576. }
  1577. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1578. cfg->rc_protocol = rtm->rtm_protocol;
  1579. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1580. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1581. cfg->rc_nlflags = nlh->nlmsg_flags;
  1582. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1583. cfg->rc_nlinfo.nlh = nlh;
  1584. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1585. for (index = 0; index <= RTA_MAX; index++) {
  1586. struct nlattr *nla = tb[index];
  1587. if (!nla)
  1588. continue;
  1589. switch (index) {
  1590. case RTA_OIF:
  1591. cfg->rc_ifindex = nla_get_u32(nla);
  1592. break;
  1593. case RTA_NEWDST:
  1594. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1595. &cfg->rc_output_labels,
  1596. cfg->rc_output_label, extack))
  1597. goto errout;
  1598. break;
  1599. case RTA_DST:
  1600. {
  1601. u8 label_count;
  1602. if (nla_get_labels(nla, 1, &label_count,
  1603. &cfg->rc_label, extack))
  1604. goto errout;
  1605. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  1606. &cfg->rc_label, extack))
  1607. goto errout;
  1608. break;
  1609. }
  1610. case RTA_GATEWAY:
  1611. NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
  1612. goto errout;
  1613. case RTA_VIA:
  1614. {
  1615. if (nla_get_via(nla, &cfg->rc_via_alen,
  1616. &cfg->rc_via_table, cfg->rc_via,
  1617. extack))
  1618. goto errout;
  1619. break;
  1620. }
  1621. case RTA_MULTIPATH:
  1622. {
  1623. cfg->rc_mp = nla_data(nla);
  1624. cfg->rc_mp_len = nla_len(nla);
  1625. break;
  1626. }
  1627. case RTA_TTL_PROPAGATE:
  1628. {
  1629. u8 ttl_propagate = nla_get_u8(nla);
  1630. if (ttl_propagate > 1) {
  1631. NL_SET_ERR_MSG_ATTR(extack, nla,
  1632. "RTA_TTL_PROPAGATE can only be 0 or 1");
  1633. goto errout;
  1634. }
  1635. cfg->rc_ttl_propagate = ttl_propagate ?
  1636. MPLS_TTL_PROP_ENABLED :
  1637. MPLS_TTL_PROP_DISABLED;
  1638. break;
  1639. }
  1640. default:
  1641. NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
  1642. /* Unsupported attribute */
  1643. goto errout;
  1644. }
  1645. }
  1646. err = 0;
  1647. errout:
  1648. return err;
  1649. }
  1650. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1651. struct netlink_ext_ack *extack)
  1652. {
  1653. struct net *net = sock_net(skb->sk);
  1654. struct mpls_route_config *cfg;
  1655. int err;
  1656. cfg = kzalloc_obj(*cfg);
  1657. if (!cfg)
  1658. return -ENOMEM;
  1659. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1660. if (err < 0)
  1661. goto out;
  1662. mutex_lock(&net->mpls.platform_mutex);
  1663. err = mpls_route_del(cfg, extack);
  1664. mutex_unlock(&net->mpls.platform_mutex);
  1665. out:
  1666. kfree(cfg);
  1667. return err;
  1668. }
  1669. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1670. struct netlink_ext_ack *extack)
  1671. {
  1672. struct net *net = sock_net(skb->sk);
  1673. struct mpls_route_config *cfg;
  1674. int err;
  1675. cfg = kzalloc_obj(*cfg);
  1676. if (!cfg)
  1677. return -ENOMEM;
  1678. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1679. if (err < 0)
  1680. goto out;
  1681. mutex_lock(&net->mpls.platform_mutex);
  1682. err = mpls_route_add(cfg, extack);
  1683. mutex_unlock(&net->mpls.platform_mutex);
  1684. out:
  1685. kfree(cfg);
  1686. return err;
  1687. }
  1688. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1689. u32 label, struct mpls_route *rt, int flags)
  1690. {
  1691. struct net_device *dev;
  1692. struct nlmsghdr *nlh;
  1693. struct rtmsg *rtm;
  1694. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1695. if (nlh == NULL)
  1696. return -EMSGSIZE;
  1697. rtm = nlmsg_data(nlh);
  1698. rtm->rtm_family = AF_MPLS;
  1699. rtm->rtm_dst_len = 20;
  1700. rtm->rtm_src_len = 0;
  1701. rtm->rtm_tos = 0;
  1702. rtm->rtm_table = RT_TABLE_MAIN;
  1703. rtm->rtm_protocol = rt->rt_protocol;
  1704. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1705. rtm->rtm_type = RTN_UNICAST;
  1706. rtm->rtm_flags = 0;
  1707. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1708. goto nla_put_failure;
  1709. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1710. bool ttl_propagate =
  1711. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1712. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1713. ttl_propagate))
  1714. goto nla_put_failure;
  1715. }
  1716. if (rt->rt_nhn == 1) {
  1717. const struct mpls_nh *nh = rt->rt_nh;
  1718. if (nh->nh_labels &&
  1719. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1720. nh->nh_label))
  1721. goto nla_put_failure;
  1722. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1723. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1724. nh->nh_via_alen))
  1725. goto nla_put_failure;
  1726. dev = nh->nh_dev;
  1727. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1728. goto nla_put_failure;
  1729. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1730. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1731. if (nh->nh_flags & RTNH_F_DEAD)
  1732. rtm->rtm_flags |= RTNH_F_DEAD;
  1733. } else {
  1734. struct rtnexthop *rtnh;
  1735. struct nlattr *mp;
  1736. u8 linkdown = 0;
  1737. u8 dead = 0;
  1738. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  1739. if (!mp)
  1740. goto nla_put_failure;
  1741. for_nexthops(rt) {
  1742. dev = nh->nh_dev;
  1743. if (!dev)
  1744. continue;
  1745. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1746. if (!rtnh)
  1747. goto nla_put_failure;
  1748. rtnh->rtnh_ifindex = dev->ifindex;
  1749. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1750. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1751. linkdown++;
  1752. }
  1753. if (nh->nh_flags & RTNH_F_DEAD) {
  1754. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1755. dead++;
  1756. }
  1757. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1758. nh->nh_labels,
  1759. nh->nh_label))
  1760. goto nla_put_failure;
  1761. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1762. nla_put_via(skb, nh->nh_via_table,
  1763. mpls_nh_via(rt, nh),
  1764. nh->nh_via_alen))
  1765. goto nla_put_failure;
  1766. /* length of rtnetlink header + attributes */
  1767. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1768. } endfor_nexthops(rt);
  1769. if (linkdown == rt->rt_nhn)
  1770. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1771. if (dead == rt->rt_nhn)
  1772. rtm->rtm_flags |= RTNH_F_DEAD;
  1773. nla_nest_end(skb, mp);
  1774. }
  1775. nlmsg_end(skb, nlh);
  1776. return 0;
  1777. nla_put_failure:
  1778. nlmsg_cancel(skb, nlh);
  1779. return -EMSGSIZE;
  1780. }
  1781. #if IS_ENABLED(CONFIG_INET)
  1782. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1783. struct fib_dump_filter *filter,
  1784. struct netlink_callback *cb)
  1785. {
  1786. return ip_valid_fib_dump_req(net, nlh, filter, cb);
  1787. }
  1788. #else
  1789. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1790. struct fib_dump_filter *filter,
  1791. struct netlink_callback *cb)
  1792. {
  1793. struct netlink_ext_ack *extack = cb->extack;
  1794. struct nlattr *tb[RTA_MAX + 1];
  1795. struct rtmsg *rtm;
  1796. int err, i;
  1797. rtm = nlmsg_payload(nlh, sizeof(*rtm));
  1798. if (!rtm) {
  1799. NL_SET_ERR_MSG_MOD(extack, "Invalid header for FIB dump request");
  1800. return -EINVAL;
  1801. }
  1802. if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
  1803. rtm->rtm_table || rtm->rtm_scope || rtm->rtm_type ||
  1804. rtm->rtm_flags) {
  1805. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
  1806. return -EINVAL;
  1807. }
  1808. if (rtm->rtm_protocol) {
  1809. filter->protocol = rtm->rtm_protocol;
  1810. filter->filter_set = 1;
  1811. cb->answer_flags = NLM_F_DUMP_FILTERED;
  1812. }
  1813. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1814. rtm_mpls_policy, extack);
  1815. if (err < 0)
  1816. return err;
  1817. for (i = 0; i <= RTA_MAX; ++i) {
  1818. int ifindex;
  1819. if (i == RTA_OIF) {
  1820. ifindex = nla_get_u32(tb[i]);
  1821. filter->dev = dev_get_by_index_rcu(net, ifindex);
  1822. if (!filter->dev)
  1823. return -ENODEV;
  1824. filter->filter_set = 1;
  1825. } else if (tb[i]) {
  1826. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
  1827. return -EINVAL;
  1828. }
  1829. }
  1830. return 0;
  1831. }
  1832. #endif
  1833. static bool mpls_rt_uses_dev(struct mpls_route *rt,
  1834. const struct net_device *dev)
  1835. {
  1836. if (rt->rt_nhn == 1) {
  1837. struct mpls_nh *nh = rt->rt_nh;
  1838. if (nh->nh_dev == dev)
  1839. return true;
  1840. } else {
  1841. for_nexthops(rt) {
  1842. if (nh->nh_dev == dev)
  1843. return true;
  1844. } endfor_nexthops(rt);
  1845. }
  1846. return false;
  1847. }
  1848. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1849. {
  1850. const struct nlmsghdr *nlh = cb->nlh;
  1851. struct net *net = sock_net(skb->sk);
  1852. struct mpls_route __rcu **platform_label;
  1853. struct fib_dump_filter filter = {
  1854. .rtnl_held = false,
  1855. };
  1856. unsigned int flags = NLM_F_MULTI;
  1857. size_t platform_labels;
  1858. unsigned int index;
  1859. int err;
  1860. rcu_read_lock();
  1861. if (cb->strict_check) {
  1862. err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
  1863. if (err < 0)
  1864. goto err;
  1865. /* for MPLS, there is only 1 table with fixed type and flags.
  1866. * If either are set in the filter then return nothing.
  1867. */
  1868. if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
  1869. (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
  1870. filter.flags)
  1871. goto unlock;
  1872. }
  1873. index = cb->args[0];
  1874. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1875. index = MPLS_LABEL_FIRST_UNRESERVED;
  1876. platform_label = mpls_platform_label_rcu(net, &platform_labels);
  1877. if (filter.filter_set)
  1878. flags |= NLM_F_DUMP_FILTERED;
  1879. for (; index < platform_labels; index++) {
  1880. struct mpls_route *rt;
  1881. rt = rcu_dereference(platform_label[index]);
  1882. if (!rt)
  1883. continue;
  1884. if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
  1885. (filter.protocol && rt->rt_protocol != filter.protocol))
  1886. continue;
  1887. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1888. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1889. index, rt, flags) < 0)
  1890. break;
  1891. }
  1892. cb->args[0] = index;
  1893. unlock:
  1894. rcu_read_unlock();
  1895. return skb->len;
  1896. err:
  1897. rcu_read_unlock();
  1898. return err;
  1899. }
  1900. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1901. {
  1902. size_t payload =
  1903. NLMSG_ALIGN(sizeof(struct rtmsg))
  1904. + nla_total_size(4) /* RTA_DST */
  1905. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1906. if (rt->rt_nhn == 1) {
  1907. struct mpls_nh *nh = rt->rt_nh;
  1908. if (nh->nh_dev)
  1909. payload += nla_total_size(4); /* RTA_OIF */
  1910. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1911. payload += nla_total_size(2 + nh->nh_via_alen);
  1912. if (nh->nh_labels) /* RTA_NEWDST */
  1913. payload += nla_total_size(nh->nh_labels * 4);
  1914. } else {
  1915. /* each nexthop is packed in an attribute */
  1916. size_t nhsize = 0;
  1917. for_nexthops(rt) {
  1918. if (!nh->nh_dev)
  1919. continue;
  1920. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1921. /* RTA_VIA */
  1922. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1923. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1924. if (nh->nh_labels)
  1925. nhsize += nla_total_size(nh->nh_labels * 4);
  1926. } endfor_nexthops(rt);
  1927. /* nested attribute */
  1928. payload += nla_total_size(nhsize);
  1929. }
  1930. return payload;
  1931. }
  1932. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1933. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1934. unsigned int nlm_flags)
  1935. {
  1936. struct sk_buff *skb;
  1937. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1938. int err = -ENOBUFS;
  1939. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1940. if (skb == NULL)
  1941. goto errout;
  1942. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1943. if (err < 0) {
  1944. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1945. WARN_ON(err == -EMSGSIZE);
  1946. kfree_skb(skb);
  1947. goto errout;
  1948. }
  1949. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1950. return;
  1951. errout:
  1952. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1953. }
  1954. static int mpls_valid_getroute_req(struct sk_buff *skb,
  1955. const struct nlmsghdr *nlh,
  1956. struct nlattr **tb,
  1957. struct netlink_ext_ack *extack)
  1958. {
  1959. struct rtmsg *rtm;
  1960. int i, err;
  1961. rtm = nlmsg_payload(nlh, sizeof(*rtm));
  1962. if (!rtm) {
  1963. NL_SET_ERR_MSG_MOD(extack,
  1964. "Invalid header for get route request");
  1965. return -EINVAL;
  1966. }
  1967. if (!netlink_strict_get_check(skb))
  1968. return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  1969. rtm_mpls_policy, extack);
  1970. if ((rtm->rtm_dst_len && rtm->rtm_dst_len != 20) ||
  1971. rtm->rtm_src_len || rtm->rtm_tos || rtm->rtm_table ||
  1972. rtm->rtm_protocol || rtm->rtm_scope || rtm->rtm_type) {
  1973. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
  1974. return -EINVAL;
  1975. }
  1976. if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
  1977. NL_SET_ERR_MSG_MOD(extack,
  1978. "Invalid flags for get route request");
  1979. return -EINVAL;
  1980. }
  1981. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1982. rtm_mpls_policy, extack);
  1983. if (err)
  1984. return err;
  1985. if ((tb[RTA_DST] || tb[RTA_NEWDST]) && !rtm->rtm_dst_len) {
  1986. NL_SET_ERR_MSG_MOD(extack, "rtm_dst_len must be 20 for MPLS");
  1987. return -EINVAL;
  1988. }
  1989. for (i = 0; i <= RTA_MAX; i++) {
  1990. if (!tb[i])
  1991. continue;
  1992. switch (i) {
  1993. case RTA_DST:
  1994. case RTA_NEWDST:
  1995. break;
  1996. default:
  1997. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
  1998. return -EINVAL;
  1999. }
  2000. }
  2001. return 0;
  2002. }
  2003. static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
  2004. struct netlink_ext_ack *extack)
  2005. {
  2006. struct net *net = sock_net(in_skb->sk);
  2007. u32 portid = NETLINK_CB(in_skb).portid;
  2008. u32 in_label = LABEL_NOT_SPECIFIED;
  2009. struct nlattr *tb[RTA_MAX + 1];
  2010. struct mpls_route *rt = NULL;
  2011. u32 labels[MAX_NEW_LABELS];
  2012. struct mpls_shim_hdr *hdr;
  2013. unsigned int hdr_size = 0;
  2014. const struct mpls_nh *nh;
  2015. struct net_device *dev;
  2016. struct rtmsg *rtm, *r;
  2017. struct nlmsghdr *nlh;
  2018. struct sk_buff *skb;
  2019. u8 n_labels;
  2020. int err;
  2021. mutex_lock(&net->mpls.platform_mutex);
  2022. err = mpls_valid_getroute_req(in_skb, in_nlh, tb, extack);
  2023. if (err < 0)
  2024. goto errout;
  2025. rtm = nlmsg_data(in_nlh);
  2026. if (tb[RTA_DST]) {
  2027. u8 label_count;
  2028. if (nla_get_labels(tb[RTA_DST], 1, &label_count,
  2029. &in_label, extack)) {
  2030. err = -EINVAL;
  2031. goto errout;
  2032. }
  2033. if (!mpls_label_ok(net, &in_label, extack)) {
  2034. err = -EINVAL;
  2035. goto errout;
  2036. }
  2037. }
  2038. if (in_label < net->mpls.platform_labels)
  2039. rt = mpls_route_input(net, in_label);
  2040. if (!rt) {
  2041. err = -ENETUNREACH;
  2042. goto errout;
  2043. }
  2044. if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
  2045. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  2046. if (!skb) {
  2047. err = -ENOBUFS;
  2048. goto errout;
  2049. }
  2050. err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
  2051. RTM_NEWROUTE, in_label, rt, 0);
  2052. if (err < 0) {
  2053. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  2054. WARN_ON(err == -EMSGSIZE);
  2055. goto errout_free;
  2056. }
  2057. err = rtnl_unicast(skb, net, portid);
  2058. goto errout;
  2059. }
  2060. if (tb[RTA_NEWDST]) {
  2061. if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
  2062. labels, extack) != 0) {
  2063. err = -EINVAL;
  2064. goto errout;
  2065. }
  2066. hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
  2067. }
  2068. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2069. if (!skb) {
  2070. err = -ENOBUFS;
  2071. goto errout;
  2072. }
  2073. skb->protocol = htons(ETH_P_MPLS_UC);
  2074. if (hdr_size) {
  2075. bool bos;
  2076. int i;
  2077. if (skb_cow(skb, hdr_size)) {
  2078. err = -ENOBUFS;
  2079. goto errout_free;
  2080. }
  2081. skb_reserve(skb, hdr_size);
  2082. skb_push(skb, hdr_size);
  2083. skb_reset_network_header(skb);
  2084. /* Push new labels */
  2085. hdr = mpls_hdr(skb);
  2086. bos = true;
  2087. for (i = n_labels - 1; i >= 0; i--) {
  2088. hdr[i] = mpls_entry_encode(labels[i],
  2089. 1, 0, bos);
  2090. bos = false;
  2091. }
  2092. }
  2093. nh = mpls_select_multipath(rt, skb);
  2094. if (!nh) {
  2095. err = -ENETUNREACH;
  2096. goto errout_free;
  2097. }
  2098. if (hdr_size) {
  2099. skb_pull(skb, hdr_size);
  2100. skb_reset_network_header(skb);
  2101. }
  2102. nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
  2103. RTM_NEWROUTE, sizeof(*r), 0);
  2104. if (!nlh) {
  2105. err = -EMSGSIZE;
  2106. goto errout_free;
  2107. }
  2108. r = nlmsg_data(nlh);
  2109. r->rtm_family = AF_MPLS;
  2110. r->rtm_dst_len = 20;
  2111. r->rtm_src_len = 0;
  2112. r->rtm_table = RT_TABLE_MAIN;
  2113. r->rtm_type = RTN_UNICAST;
  2114. r->rtm_scope = RT_SCOPE_UNIVERSE;
  2115. r->rtm_protocol = rt->rt_protocol;
  2116. r->rtm_flags = 0;
  2117. if (nla_put_labels(skb, RTA_DST, 1, &in_label))
  2118. goto nla_put_failure;
  2119. if (nh->nh_labels &&
  2120. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  2121. nh->nh_label))
  2122. goto nla_put_failure;
  2123. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  2124. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  2125. nh->nh_via_alen))
  2126. goto nla_put_failure;
  2127. dev = nh->nh_dev;
  2128. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  2129. goto nla_put_failure;
  2130. nlmsg_end(skb, nlh);
  2131. err = rtnl_unicast(skb, net, portid);
  2132. errout:
  2133. mutex_unlock(&net->mpls.platform_mutex);
  2134. return err;
  2135. nla_put_failure:
  2136. nlmsg_cancel(skb, nlh);
  2137. err = -EMSGSIZE;
  2138. errout_free:
  2139. mutex_unlock(&net->mpls.platform_mutex);
  2140. kfree_skb(skb);
  2141. return err;
  2142. }
  2143. static int resize_platform_label_table(struct net *net, size_t limit)
  2144. {
  2145. size_t size = sizeof(struct mpls_route *) * limit;
  2146. size_t old_limit;
  2147. size_t cp_size;
  2148. struct mpls_route __rcu **labels = NULL, **old;
  2149. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  2150. unsigned index;
  2151. if (size) {
  2152. labels = kvzalloc(size, GFP_KERNEL);
  2153. if (!labels)
  2154. goto nolabels;
  2155. }
  2156. /* In case the predefined labels need to be populated */
  2157. if (limit > MPLS_LABEL_IPV4NULL) {
  2158. struct net_device *lo = net->loopback_dev;
  2159. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  2160. if (IS_ERR(rt0))
  2161. goto nort0;
  2162. rt0->rt_nh->nh_dev = lo;
  2163. netdev_hold(lo, &rt0->rt_nh->nh_dev_tracker, GFP_KERNEL);
  2164. rt0->rt_protocol = RTPROT_KERNEL;
  2165. rt0->rt_payload_type = MPT_IPV4;
  2166. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2167. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2168. rt0->rt_nh->nh_via_alen = lo->addr_len;
  2169. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  2170. lo->addr_len);
  2171. }
  2172. if (limit > MPLS_LABEL_IPV6NULL) {
  2173. struct net_device *lo = net->loopback_dev;
  2174. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  2175. if (IS_ERR(rt2))
  2176. goto nort2;
  2177. rt2->rt_nh->nh_dev = lo;
  2178. netdev_hold(lo, &rt2->rt_nh->nh_dev_tracker, GFP_KERNEL);
  2179. rt2->rt_protocol = RTPROT_KERNEL;
  2180. rt2->rt_payload_type = MPT_IPV6;
  2181. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2182. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2183. rt2->rt_nh->nh_via_alen = lo->addr_len;
  2184. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  2185. lo->addr_len);
  2186. }
  2187. mutex_lock(&net->mpls.platform_mutex);
  2188. /* Remember the original table */
  2189. old = mpls_dereference(net, net->mpls.platform_label);
  2190. old_limit = net->mpls.platform_labels;
  2191. /* Free any labels beyond the new table */
  2192. for (index = limit; index < old_limit; index++)
  2193. mpls_route_update(net, index, NULL, NULL);
  2194. /* Copy over the old labels */
  2195. cp_size = size;
  2196. if (old_limit < limit)
  2197. cp_size = old_limit * sizeof(struct mpls_route *);
  2198. memcpy(labels, old, cp_size);
  2199. /* If needed set the predefined labels */
  2200. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  2201. (limit > MPLS_LABEL_IPV6NULL)) {
  2202. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  2203. rt2 = NULL;
  2204. }
  2205. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  2206. (limit > MPLS_LABEL_IPV4NULL)) {
  2207. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  2208. rt0 = NULL;
  2209. }
  2210. /* Update the global pointers */
  2211. local_bh_disable();
  2212. write_seqcount_begin(&net->mpls.platform_label_seq);
  2213. net->mpls.platform_labels = limit;
  2214. rcu_assign_pointer(net->mpls.platform_label, labels);
  2215. write_seqcount_end(&net->mpls.platform_label_seq);
  2216. local_bh_enable();
  2217. mutex_unlock(&net->mpls.platform_mutex);
  2218. mpls_rt_free(rt2);
  2219. mpls_rt_free(rt0);
  2220. if (old) {
  2221. synchronize_rcu();
  2222. kvfree(old);
  2223. }
  2224. return 0;
  2225. nort2:
  2226. mpls_rt_free(rt0);
  2227. nort0:
  2228. kvfree(labels);
  2229. nolabels:
  2230. return -ENOMEM;
  2231. }
  2232. static int mpls_platform_labels(const struct ctl_table *table, int write,
  2233. void *buffer, size_t *lenp, loff_t *ppos)
  2234. {
  2235. struct net *net = table->data;
  2236. int platform_labels = net->mpls.platform_labels;
  2237. int ret;
  2238. struct ctl_table tmp = {
  2239. .procname = table->procname,
  2240. .data = &platform_labels,
  2241. .maxlen = sizeof(int),
  2242. .mode = table->mode,
  2243. .extra1 = SYSCTL_ZERO,
  2244. .extra2 = &label_limit,
  2245. };
  2246. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2247. if (write && ret == 0)
  2248. ret = resize_platform_label_table(net, platform_labels);
  2249. return ret;
  2250. }
  2251. #define MPLS_NS_SYSCTL_OFFSET(field) \
  2252. (&((struct net *)0)->field)
  2253. static const struct ctl_table mpls_table[] = {
  2254. {
  2255. .procname = "platform_labels",
  2256. .data = NULL,
  2257. .maxlen = sizeof(int),
  2258. .mode = 0644,
  2259. .proc_handler = mpls_platform_labels,
  2260. },
  2261. {
  2262. .procname = "ip_ttl_propagate",
  2263. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  2264. .maxlen = sizeof(int),
  2265. .mode = 0644,
  2266. .proc_handler = proc_dointvec_minmax,
  2267. .extra1 = SYSCTL_ZERO,
  2268. .extra2 = SYSCTL_ONE,
  2269. },
  2270. {
  2271. .procname = "default_ttl",
  2272. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  2273. .maxlen = sizeof(int),
  2274. .mode = 0644,
  2275. .proc_handler = proc_dointvec_minmax,
  2276. .extra1 = SYSCTL_ONE,
  2277. .extra2 = &ttl_max,
  2278. },
  2279. };
  2280. static __net_init int mpls_net_init(struct net *net)
  2281. {
  2282. size_t table_size = ARRAY_SIZE(mpls_table);
  2283. struct ctl_table *table;
  2284. int i;
  2285. mutex_init(&net->mpls.platform_mutex);
  2286. seqcount_mutex_init(&net->mpls.platform_label_seq, &net->mpls.platform_mutex);
  2287. net->mpls.platform_labels = 0;
  2288. net->mpls.platform_label = NULL;
  2289. net->mpls.ip_ttl_propagate = 1;
  2290. net->mpls.default_ttl = 255;
  2291. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  2292. if (table == NULL)
  2293. return -ENOMEM;
  2294. /* Table data contains only offsets relative to the base of
  2295. * the mdev at this point, so make them absolute.
  2296. */
  2297. for (i = 0; i < table_size; i++)
  2298. table[i].data = (char *)net + (uintptr_t)table[i].data;
  2299. net->mpls.ctl = register_net_sysctl_sz(net, "net/mpls", table,
  2300. table_size);
  2301. if (net->mpls.ctl == NULL) {
  2302. kfree(table);
  2303. return -ENOMEM;
  2304. }
  2305. return 0;
  2306. }
  2307. static __net_exit void mpls_net_exit(struct net *net)
  2308. {
  2309. struct mpls_route __rcu **platform_label;
  2310. size_t platform_labels;
  2311. const struct ctl_table *table;
  2312. unsigned int index;
  2313. table = net->mpls.ctl->ctl_table_arg;
  2314. unregister_net_sysctl_table(net->mpls.ctl);
  2315. kfree(table);
  2316. /* An rcu grace period has passed since there was a device in
  2317. * the network namespace (and thus the last in flight packet)
  2318. * left this network namespace. This is because
  2319. * unregister_netdevice_many and netdev_run_todo has completed
  2320. * for each network device that was in this network namespace.
  2321. *
  2322. * As such no additional rcu synchronization is necessary when
  2323. * freeing the platform_label table.
  2324. */
  2325. mutex_lock(&net->mpls.platform_mutex);
  2326. platform_label = mpls_dereference(net, net->mpls.platform_label);
  2327. platform_labels = net->mpls.platform_labels;
  2328. for (index = 0; index < platform_labels; index++) {
  2329. struct mpls_route *rt;
  2330. rt = mpls_dereference(net, platform_label[index]);
  2331. mpls_notify_route(net, index, rt, NULL, NULL);
  2332. mpls_rt_free(rt);
  2333. }
  2334. mutex_unlock(&net->mpls.platform_mutex);
  2335. kvfree(platform_label);
  2336. }
  2337. static struct pernet_operations mpls_net_ops = {
  2338. .init = mpls_net_init,
  2339. .exit = mpls_net_exit,
  2340. };
  2341. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  2342. .family = AF_MPLS,
  2343. .fill_stats_af = mpls_fill_stats_af,
  2344. .get_stats_af_size = mpls_get_stats_af_size,
  2345. };
  2346. static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_module = {
  2347. {THIS_MODULE, PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL,
  2348. RTNL_FLAG_DOIT_UNLOCKED},
  2349. {THIS_MODULE, PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL,
  2350. RTNL_FLAG_DOIT_UNLOCKED},
  2351. {THIS_MODULE, PF_MPLS, RTM_GETROUTE, mpls_getroute, mpls_dump_routes,
  2352. RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
  2353. {THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
  2354. mpls_netconf_get_devconf, mpls_netconf_dump_devconf,
  2355. RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
  2356. };
  2357. static int __init mpls_init(void)
  2358. {
  2359. int err;
  2360. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  2361. err = register_pernet_subsys(&mpls_net_ops);
  2362. if (err)
  2363. goto out;
  2364. err = register_netdevice_notifier(&mpls_dev_notifier);
  2365. if (err)
  2366. goto out_unregister_pernet;
  2367. dev_add_pack(&mpls_packet_type);
  2368. err = rtnl_af_register(&mpls_af_ops);
  2369. if (err)
  2370. goto out_unregister_dev_type;
  2371. err = rtnl_register_many(mpls_rtnl_msg_handlers);
  2372. if (err)
  2373. goto out_unregister_rtnl_af;
  2374. err = ipgre_tunnel_encap_add_mpls_ops();
  2375. if (err) {
  2376. pr_err("Can't add mpls over gre tunnel ops\n");
  2377. goto out_unregister_rtnl;
  2378. }
  2379. err = 0;
  2380. out:
  2381. return err;
  2382. out_unregister_rtnl:
  2383. rtnl_unregister_many(mpls_rtnl_msg_handlers);
  2384. out_unregister_rtnl_af:
  2385. rtnl_af_unregister(&mpls_af_ops);
  2386. out_unregister_dev_type:
  2387. dev_remove_pack(&mpls_packet_type);
  2388. unregister_netdevice_notifier(&mpls_dev_notifier);
  2389. out_unregister_pernet:
  2390. unregister_pernet_subsys(&mpls_net_ops);
  2391. goto out;
  2392. }
  2393. module_init(mpls_init);
  2394. static void __exit mpls_exit(void)
  2395. {
  2396. rtnl_unregister_all(PF_MPLS);
  2397. rtnl_af_unregister(&mpls_af_ops);
  2398. dev_remove_pack(&mpls_packet_type);
  2399. unregister_netdevice_notifier(&mpls_dev_notifier);
  2400. unregister_pernet_subsys(&mpls_net_ops);
  2401. ipgre_tunnel_encap_del_mpls_ops();
  2402. }
  2403. module_exit(mpls_exit);
  2404. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  2405. MODULE_LICENSE("GPL v2");
  2406. MODULE_ALIAS_NETPROTO(PF_MPLS);