af_mpls.c 57 KB

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